The kingpin behind the latest corruption scandal to roil the New York Police Department is a retired detective who allegedly ran a ring of brothels and gambling dens with the help of active cops who were paid in cash and sex for muscle and tip-offs.

In announcing charges on Thursday, prosecutors said Ludwig Paz, 51, used his Vice Squad know-how and badge-wearing buddies to avoid a law-enforcement crackdown on the multimillion-dollar operation.

For instance, he knew from his training that undercover detectives are not allowed to expose their genitals during interactions with prostitutes during investigations.

So he devised a detailed screening process that required new brothel clients to undress and allow themselves to be fondled, proving they weren’t lawmen, authorities said.

“ Am I angry? Of course I am angry. ” — NYPD Commissioner James O’Neill

Paz allegedly relied on his contacts within the NYPD, enlisting the help of at least seven officers who helped with daily duties that ranged from providing protection to leaking confidential police information about possible raids. One of them did it for just a discount on sex with a prostitute, said Queens Assistant District Attorney Gerard Brave, the head of the Organized Crime and Rackets Bureau.

“Am I angry? Of course I am angry,” said Police Commissioner James O’Neill on Thursday, noting that the scandal was not a department-wide problem but the work of a small group that “tarnished” the good work of tens of thousands of others in the department.

He said the NYPD has been more aggressive over the last several years in trying to root out corruption.

“We have done a lot to build up trust all over the city,” O’Neill said. “By taking this step and being transparent, [we are] acknowledging that there are people out there that wear shields and do break the laws. There are consequences, there are severe consequences for everyone involved in this case.”

The latest arrests mark the most significant NYPD scandal since cops, some of them in top jobs, were hit with federal corruption charges last year for trading handgun licenses for cash, prostitutes, and expensive trips.

The brothel investigation, dubbed Operation Zap, began in April 2015 after a uniformed police officer told the Internal Affairs Bureau at NYPD headquarters that “someone who may be involved in illegal activities was having conversations with someone in the vice enforcement division,” said Joseph Reznick, the head of the NYPD’s Internal Affairs Bureau.

That someone, authorities say, turned out to be Paz, who became a cop in 1990 and joined a vice unit in 2001. He retired from the NYPD as a detective “in good standing” in 2010, collecting a full a pension.

The investigation involved hundreds of hours of police work, undercover investigators, physical surveillance and court-ordered electronic surveillance—the same wire taps used in organized crimes cases—to determine the structure and scope of Paz’s operation, authorities said.

“It was only because we were up on so many phones that we were able to get the full scope of the corruption,” Brave said.

What they found wouldn’t be out of place in a script for the gritty HBO show The Deuce–with Paz’s buddies in blue having sex in the brothels on and off duty, he said.

Paz controlled brothels in Queens, Brooklyn, and Long Island, authorities said. Working with his wife, Arelis Peralta, he also set up a number of gambling rooms in businesses, including beauty salons, in Queens and Brooklyn, they said.

He was able to keep operating even though the shenanigans were obvious to local residents.

A local craftsman who works near one brothel told The Daily Beast that he reported suspicious activity to a women’s organization about five years ago, and the group passed on the info to an NYPD official involved in investigating human trafficking.

“There was a constant stream of guys that would come by, on their cells,” said the man, who didn’t want his name used due to safety concerns. “They’d go in and come out 20 minutes later.”

“ By taking this step and being transparent, [we are] acknowledging that there are people out there that wear shields and do break the laws. ” — NYPD Commissioner James O’Neill

As part of the investigation, Reznick said authorities looked at whether any of the prostitutes had been trafficked and determined “all of them appeared to have volunteered for that type of work.”

Another man who lives near the Brooklyn brothel said the prostitution was an open secret. “It was a known thing,” he said. “You’d see scantily dressed women show up at the same time.”

The probe revealed that the prostitution ring collected more than $2 million between August 2016 and September 2017, and that the brothels used online ads to attract customers, who paid $40 for 15 minutes and $160 for a full hour of sex, police said.

It culminated Wednesday when Paz, the seven officers, and roughly three dozen people outside the NYPD were charged. Two other officers who face departmental charges have been stripped of their badges and guns, and placed on desk duty, and police believe there may be others involved who face internal charges.

Paz became friends with the other defendants while he was a cop and personally knew each one of the officers who has been charged or disciplined, officials said.

“If a book was ever written about this case, I would probably name it Loyalty or Disloyalty Versus Friendship, because that’s what it came down to, friendship with a retired member of the service, maintaining the friendship with active members of the service,” Reznick said.

Paz’s alleged accomplices included Brooklyn South Vice Det. Rene Samaniego, 43, who is accused of helping Paz with both the prostitution and the gambling rings; and Sgt. Carlos Cruz, 41, and Det. Giovanny Rojas Acosta, 40, who are accused of giving Paz information on law enforcement activities. The three are all charged with enterprise corruption.

Sgt. Cliff Nieves, 37, and his brother Sgt. Steven Nieves, 32, were charged with promoting prostitution after they allegedly operated a brothel for a one-night bachelor party. Police Officer Giancarlo Raspanti is been charged with giving Paz confidential police information in exchange for discounted sex at a brothel, while Police Officer Louis Failla is accused of helping Paz after a brothel was raided.

Reznick said the officers provided information on active and ongoing investigations, misused department computers by conducting unauthorized inquiries on people and locations, and provided information including the photo of an informant.

He said other current and retired officers are under scrutiny, but unless new information is uncovered, he expects they will face only internal discipline.

—With reporting from Victoria Bekiempis