Who’d have guessed that, as far back as the 1930s, it was the mob who would give homosexuals a place to mingle, hook up and eventually coalesce as a movement — by running the city’s underground gay bars.

Two conditions brought these seemingly oppositional groups together. One: It was illegal to be gay, with police routinely hauling in homosexuals on charges of lewdness or indecency. Two: The Mafia, principally Vito Genovese, controlled Manhattan’s West Side, including the Village.

In short, a gay bar was an illegal business — or at a minimum, a business subject to relentless harassment. Yet where most New Yorkers saw deviance, the Mafia saw profit. Same as with gambling, prostitution or bootlegging, all it took was the customary payoffs for cops to look the other way.

“New York State’s liquor laws barred ‘disorderly’ premises,” writes C. Alexander Hortis in his new history, “The Mob and the City,” adding that the State Liquor Authority and NYPD used this excuse to close hundreds of bars in the 1930s and ’40s that catered to “homosexuals soliciting partners.”

Enter the wiseguys. It was Mafia bosses who founded hot spots, from the famed Stonewall Inn to the lesbian haunt the Howdy Club to the 181 Club, known as “the homosexual Copacabana.” And it was Mafia muscle running the clubs behind the scenes. In La Cosa Nostra-speak, the finocchio — “fairies” — were good business.

“I was the safest on the streets of New York that I had ever been,” said one gay-club bartender. “If anybody ever threatened me or intimidated me, I had recourse. I had been stopped by the police and . . . all I had to do was give them the name of my employer and they let me go, because we were both working for the same people.

“The law made the gay bars illegal. The Family made it operable.”

In particular, the Stonewall Inn holds a peculiar place in the confluence of gays coming out and goombahs staying out of sight. The largest gay bar in America, it opened in 1967 with the backing of the Genovese crime family. It had no fire exits, no running water to wash glasses, and the toilets routinely overflowed. But it was the only gay bar in the city where dancing was tolerated by bought-off policemen. And so the Stonewall became an institution.

But there’s always a price when you’re doing business with the Mafia. In this case, the No. 1 gripe by customers was that the drinks were atrocious.

“Some of the funniest stories are about the awful liquor supplied by the Mafia,” Hortis tells The Post. “Wiseguys stole shipments of liquor and watered it down heavily.”

One patron recalled: “I never bought a drink at the Stonewall. Never, never, never. Mafia house beer? I mean, does anyone know what that is.”

Chuck Shaheen, the legit face of the Stonewall, admitted, “None of the liquor was brand-name. We would go in the back at the beginning of our shift and take Dewar’s bottles and pour whatever swill we could get into it. The same with vodka. I mean, it wouldn’t be Smirnoff. Nothing would be what it said.”

Regardless of weak drinks, with a near-monopoly on a growing clientele, gay bars became a Mafia cash cow. The cigarette vending machines were filled with bootlegged smokes. Any thoughts of income-tax payments or liquor licenses were a joke. All for a monthly payoff to the 6th Precinct of $1,200.

Police raids persisted, mostly at the insistence of neighbors offended by the presence of the gay bars. But at the Stonewall, Genovese capo Matty “The Horse” Ianniello was always tipped off in advance. Liquor was stashed away. And whatever raids were made for the sake of appearances occurred early enough in the evening that business could resume in full a few hours later.

The ugly breakup of gays and goombahs began, not coincidentally, in the late ’60s, along with the upswell in gay activism.

Calls for honest-run businesses began, led by the Mattachine Society, the city’s first gay-rights organization.

But of more significance was the infamous police raid on the Stonewall Inn in June 1969. The raid sparked a historic riot that’s credited with truly igniting the gay uprising — yet Hortis posits that it was really a crackdown on the Mafia and that the gay patrons bloodied that night by billy clubs were mostly just in the wrong place.

The cop who turned his back on the mob’s payoffs and led the raid, Deputy Inspector Seymour Pine, said later, “We weren’t concerned about the gays. We were concerned about the Mafia.”

Whether that was police spin or not, the Stonewall riots famously fueled a rights movement — but also ended the mutually beneficial relationship with the mob.

Somebody even wrote in chalk on the Stonewall’s boarded-up windows: “Gay Prohibition Corupt$ Cop$ and Feed$ Mafia.”