The complaints from neighbors about prostitution flourishing behind the Brooklyn apartment’s thin walls had been flowing into the vice squad for months. Men, arriving at all hours, were asked at the door which nationality of women they preferred: “Brazil? Peru?”

Police officers swooped in one day in September 2017, slamming through the door with a battering ram, but they left empty-handed. The brothel on the border of Gowanus and Park Slope had quietly closed down before the raid, just as had happened with several other brothels around the borough, gone dark right before the police showed up.

The brothel empire was always one step ahead of the law.

Last week, prosecutors disclosed why: They said the brothels were run by a retired police detective who had been repeatedly tipped off about planned raids by officers on the force, revealing one of the worst corruption scandals to hit the New York Police Department in years.

The retired detective, Ludwig Paz, 51, was arrested and accused of running a broad and complex syndicate of prostitution and gambling that spanned Brooklyn and Queens and brought in millions of dollars. Three sergeants, two detectives and two officers were also charged. Two other officers were stripped of their guns and shields and placed on administrative duty. Dozens of civilians were arrested, and more are being sought.