The U.S. Supreme Court ruled on Thursday in favor of a religious-affiliated foster care agency seeking to refuse child placement into LGBTQ homes, determining the City of Philadelphia’s enforcement of a contract with non-discrimination provisions violates freedom of religion under the First Amendment.

In a surprise twist, the ruling was unanimous with nine justices on the court agreeing to the result in favor of Catholic Social Services, with Chief Justice John Roberts writing the opinion. As noted by SCOTUSblog, the court seemed much more divided in oral arguments, although inclined to rule for the foster care agency.

“The refusal of Philadelphia to contract with CSS for the provision of foster care services unless the agency agrees to certify same-sex couples as foster parents cannot survive strict scrutiny and violates the Free Exercise Clause of the First Amendment,” Roberts writes.

Although Catholic Social Services had also contended a freedom of speech right under the First Amendment to reject same-sex couples, Roberts adds the court didn’t reach a conclusion on that part of the argument.

Marianne Duddy-Burke, executive director of the Catholic LGBTQ group DignityUSA, condemned the decision in a statement immediately after it was handed down.

“Today, the well-being of our country’s most vulnerable children has been sacrificed to preserve tax-payer funded discrimination for a powerful group of religious institutions,” Duddy-Burke said. “The Supreme Court just decreased the number of homes available to our youth in foster care, making what was already a crisis worse. Same-sex couples are seven times more likely than straight couples to adopt or be foster parents and are more likely to have trans-racial families. This ruling means tens of thousands of children may never have a family to love and support them.”

Becket Law, which had argued in case on behalf of Catholic Social Services, crowed in a statement over its win at the Supreme Court.

“It’s a beautiful day when the highest court in the land protects foster moms and the 200-year-old religious ministry that supports them,” said Lori Windham, senior counsel at Becket. “Taking care of children, especially children who have been neglected and abused is a universal value that spans all ideological divides.

A key portion of the Roberts decision that could limit its reach is language specific to Philadelphia’s contract with the city allowing for discretion on enforcement, which he says means the measure isn’t generally applicable measure.

“Section 3.21 of the contract requires an agency to provide services defined in the contract to prospective foster parents without regard to their sexual orientation,” Roberts writes. “But section 3.21 also permits exceptions to this requirement at the ‘sole discretion’ of the Commissioner. This inclusion of a mechanism for entirely discretionary exceptions renders the non-discrimination provision not generally applicable.”

The American Civil Liberties Union, which had argued before the Supreme Court in the case and sided with the City of Philadelphia, claimed a small victory after the decision.

“The decision will not affect any foster care programs that do not have the same system for individualized exemptions that were at issue here,” Leslie Cooper, deputy director of the ACLU LGBTQ & HIV Project, said in a statement. “This is good news for the more than 400,000 children in foster care across the country, who are the ones who get hurt the most if placement decisions are made based on an agency’s religious beliefs rather than the child’s best interest. And this decision does not allow discrimination in other taxpayer-funded government programs such as homeless shelters, disaster relief programs and health care.”

David Flugman, a lawyer at the New York-based Selendy & Gay PLLC whose practice includes LGBTQ rights, said in a statement the technical nature of the Fulton is “sure to invite even more litigation.“

“Today the Supreme Court held, on narrow, technical grounds, that the City of Philadelphia’s attempt to ensure that Catholic Charities abide by the same non-discrimination provisions applicable to all other city contractors could not withstand Catholic Charities’ religious right to refuse to screen loving same-sex couples to act as foster parents,” Flugman writes. “The Court did not take up Catholic Charities’ invitation to scuttle the 30 year-old test for free exercise claims that was announced in Smith v. Employment Division, which held that a neutral law of general applicability could survive even if it burdens religious practice.”

The Supreme Court reversed and remanded decision of the U.S. Third Circuit of Court of Appeals, which had ruled in favor of City of Philadelphia enforcing its contract with Catholic Social Services. Both the appeals courts and the lower trial court had come to the opposite conclusion of the U.S. Supreme Court.

Notably, although the City of Philadelphia in addition to the contract it struck with Catholic Social Services has in a place LGBTQ non-discrimination ordinance, the Supreme Court determines that measure doesn’t apply in the context of foster care services because it’s limited to the services “made available to the public.”

“Certification is not ‘made available to the public’ in the usual sense of the words,” Roberts writes. “Certification as a foster parent is not readily accessible to the public; the process involves a customized and selective assessment that bears little resemblance to staying in a hotel, eating at a restaurant, or riding a bus.”

Fatima Goss Graves, CEO of the National Women’s Law Center, said in a statement the decision from the Supreme Court “is a harmful loss to the children in the foster care system in Philadelphia as well as the countless LGBTQ parents.”

“Weakening the government’s ability to protect their civil rights is hardly in their best interest, and we’re committed to ensuring this loophole is not stretched to further justify hatred or prejudice,” Graves added. “We must protect the right of every person to live without fear of discrimination because of who they are or who they love, and we must hold that value particularly close when it comes to the best interest of LGBTQ youth and the families who love them.”

This article originally appeared in the Washington Blade and is made available in partnership with the National LGBT Media Association.