One Long Island real estate agent told a black man that houses in a predominantly white neighborhood were too expensive for his budget. But the same agent showed houses in the same neighborhood to a white man with the same amount of money to spend.

Another real estate agent warned a white homebuyer about gang violence in a mostly minority neighborhood, but she appeared to steer a black buyer with a comparable budget toward homes in that neighborhood.

All told, real estate agents treated people of color unequally 40% of the time compared with white people when they searched for homes on Long Island, one of the most racially segregated suburbs in the United States.

Those were among the findings of a three-year undercover investigation published this past weekend by Newsday, a Long Island newspaper, which exposed widespread evidence that discriminatory, and potentially illegal, home-selling practices are helping to keep the area’s neighborhoods segregated.

Newsday did the sort of undercover testing that government agencies and nonprofit groups are supposed to do to enforce fair-housing regulations meant to prevent disparate treatment based on racial and other factors.

The newspaper recruited a diverse group of people, some of them actors, to pose as first-time homebuyers, outfitting them with hidden cameras to capture their interactions with real estate agents. It is legal in New York to record a conversation or phone call if the person making the recording is a party to the conversation.

Over 16 months, the would-be homebuyers conducted about 100 tests. They got help from housing experts and recorded 240 hours of video that the newspaper turned into a 13-article series and a 40-minute documentary. More than 50 reporters, producers, editors and other Newsday employees worked on the project.

The newspaper contacted 93 real estate agents at some of the area’s biggest firms. Two testers — of different races, but of the same gender and about the same age, with similar income and housing preferences — would independently approach the same agent to test whether they were treated differently based on their race.

The results were startling, even though Long Island is a part of New York where redlining, zoning regulations and other forces have long perpetuated racial divides.

“Black testers experienced disparate treatment 49 percent of the time — compared with 39 percent for Hispanic and 19 percent for Asian testers,” Newsday reported.

In some cases, agents did not show listings to black testers if they had not been preapproved for a mortgage. The same agents would accommodate white testers who did not meet that condition.

The federal fair-housing law, passed in 1968 as part of the Civil Rights Act, prohibits discrimination in housing based on race, religion and other factors.

The Department of Housing and Urban Development is responsible for enforcing the law, mostly through its regional offices and through grants to nonprofits to investigate complaints, conduct tests and educate buyers and renters.

In New York, state agencies also enforce the law, with the state’s 133,000 licensed real estate brokers and agents responsible for abiding by fair-housing standards.

The enforcement process, which can be slow, starts mostly with complaints, but undercover tests are often the most effective way of proving disparate treatment. Newsday found that tests were used only sporadically because they are time-consuming and expensive.

Records show few examples of the state imposing heavy penalties, like fines, on agents found to have discriminated, Newsday found.

Government officials said they were examining what Newsday had uncovered.

“We are reviewing this report, but make no mistake: Every complaint received is thoroughly investigated and we urge any New Yorker who believes they have been the victim of housing discrimination to contact us immediately,” Richard Azzopardi, a spokesman for Gov. Andrew Cuomo, said in a statement.

Olga Alvarez, a spokeswoman for the federal housing department, said that the agency was examining Newsday’s findings.

“Combating housing discrimination is at the forefront of our department’s enforcement priorities,” she said.

Ken Zimmerman, a distinguished fellow and fair housing expert at New York University’s Furman Center, said that Newsday’s findings were a “modern manifestation of what has gotten in the way of the legal mandate and policy aspirations of the Fair Housing Act.”

“There needs to be more investment in uncovering intentional discrimination, as these tests reflect,” he added.

Real estate agents are barred from talking to clients about the backgrounds of people who live in neighborhoods and they are required to provide equal guidance about areas in which they may want to live.

Newsday found that agents ignored those rules repeatedly.

Agents overwhelmingly directed white buyers to Merrick, a hamlet in Nassau County where most of the residents are white. When discussing Brentwood, a predominantly black and Hispanic hamlet where gangs like MS-13 are active, an agent told one black tester that residents there were the “nicest people.”

The same agent emailed a white tester to “please kindly do some research on the gang related events in that area for safety.”

White customers were often given more listings than minority testers, Newsday found. And in a quarter of the cases, agents appeared to unlawfully steer buyers to different communities based on their race or ethnicity.

“This investigation brought to light what too many people of color in our state know already,” Sen. Kirsten Gillibrand, D-N.Y., said in a statement, “that discrimination is real and targeted, and rears its head in every aspect of their lives.”

This article originally appeared in The New York Times.