A federal court in Pennsylvania on Friday sided with the city of Philadelphia, saying that the city had the right to terminate a foster care agency's contract after it refused to place children with same-sex couples.

In March, Philadelphia stopped referring children to two foster care providers after learning that they had refused to license same-sex couples to be foster parents.

Catholic Social Services (CSS) sued the city, arguing that its right to free exercise of religion and free speech were being violated.

On Friday, the U.S. District Court for the Eastern District of Pennsylvania sided with the city, refusing CSS's demand that it issue a preliminary injunction ordering Philadelphia to resume referrals of cases to CSS.

In June, the American Civil Liberties Union (ACLU) and the ACLU of Pennsylvania filed a motion to intervene in the lawsuit on behalf of several non-profit organizations, including Philadelphia Family Pride, which represents gay and lesbian foster parents and prospective foster parents.

“First and foremost, this is a victory for children in Philadelphia who need a loving home and can’t afford to have good families turned away for failing to meet a religious litmus test,” said Reggie Shuford, executive director of the American Civil Liberties Union of Pennsylvania. “We’re proud that the city is committed to ensuring that no qualified family that comes forward to care for a child in need is turned away because of their sexual orientation or other reasons unrelated to the ability to care for a child. And we’re thrilled that the court rejected the claimed constitutional right to discriminate against loving families.”

Several states have passed laws allowing adoption and foster care agencies to refuse gay couples, including Alabama, Michigan, Mississippi, North Dakota, South Dakota, Texas, Virginia, Oklahoma and Kansas. On Wednesday, an amendment to a funding bill with similar goals cleared a House committee.

(Related: Amendment allowing adoption agencies to refuse gay couples clears House panel.)

Such laws are believed to be part of a strategy to undermine marriage laws as they relate to same-sex couples and the Supreme Court's 2015 Obergefell ruling, which found that gay couples have a constitutional right to marry.

According to the ACLU, Friday's ruling may impact such laws.

“When faith-based agencies choose to receive taxpayer dollars to provide public child welfare services, their religious beliefs cannot trump the best interests of the children in their care,” said Leslie Cooper, deputy director of the ACLU’s LGBT and HIV Project. “The court saw it was not only permissible for the city of Philadelphia to prohibit discrimination by its contract agencies, but that allowing the use of religious screening requirements in the public child welfare system would likely violate the Constitution.”