The latest Supreme Court conflict between protecting religious rights and fighting discrimination against gay couples divided and even seemed to perturb some of the justices Wednesday.

In a teleconference hearing, they took up a case from Philadelphia, where city officials ended a contract to provide foster care services with Catholic Social Services. The agency said it would not accept applications from married same-sex couples who wanted to be foster parents, citing religious beliefs about marriage.

The U.S. Court of Appeals for the 3rd Circuit ruled the city was within its rights to end the contract and was not targeting the Catholic agency for its religious views. Instead, the court said, the city was insisting that those with whom it does business agree with its nondiscrimination policy.

Justice Samuel A. Alito Jr. was not convinced.

“If we are honest about what’s really going on here, it’s not about ensuring that same-sex couples in Philadelphia have the opportunity to be foster parents,” Alito said to Washington lawyer Neal K. Katyal, who was representing the city.

“It’s the fact that the city can’t stand the message that Catholic Social Services and the archdiocese are sending by continuing to adhere to the old-fashioned view about marriage. Isn’t that the case?”

“Absolutely not,” Katyal replied. “Going evidence by evidence, piece by piece, they [lower courts] rejected that idea.”

Justice Elena Kagan, coming from the other side, pressed Justice Department lawyer Hashim M. Mooppan four times to answer a variation of the question: “Do you think it is a compelling state interest to want to eradicate discrimination against gays and lesbians?”

Mooppan, representing the Trump administration’s support of Catholic Social Services, eventually said that government might have such an interest “in the abstract,” but that was not Philadelphia’s only motivation.

It was the first major case for Justice Amy Coney Barrett, who had alarmed gay rights activists during her confirmation hearings by refusing to endorse some of the court’s landmark decisions on LGBTQ rights, including the 2015 decision in Obergefell v. Hodges that found a constitutional right for same-sex couples to marry. Barrett said she was simply refusing to pass judgment on the court’s work.

The newest justice asked both welcome and tough questions of both sides in Wednesday’s hearing.

It was easier to read the motivations of other justices. Justice Clarence Thomas, along with Alito, gently questioned CSS’s lawyer Lori H. Windham to show that the agency had never been approached by a gay couple seeking the home vetting it provides prospective foster parents.

“Zero” was the answer from Windham, a lawyer for the Becket Fund for Religious Liberty, when asked how many same-sex couples had been denied the opportunity to be foster parents because of CSS policy. She said if ever approached, the agency would refer the couple to one of the more than two dozen agencies that have no issue with same-sex marriage.

“The city has no compelling reason for excluding Catholic Social Services, which has exercised its faith by serving at-risk children in Philadelphia for two centuries,” Windham said. “Nor does it have any interest in refusing to allow the agency to step aside and provide referrals elsewhere. Yet, Philadelphia is refusing to place children with loving mothers . . . just because they chose to partner with an agency who shares their faith.”

Philadelphia learned from a reporter that the agency and one other on a city contract refused to work with same-sex couples. The city said that violated its 40-year-old Fair Practices Ordinance, which prohibits discrimination based on sexual orientation.

The other agency changed its policy, but CSS said it would not.

Justice Brett M. Kavanaugh accused the city of “looking for a fight” rather than trying to come up with a “win-win” compromise.

He said when the court decided Obergefell, there was an explicit promise to take into account religious disagreement about the meaning of marriage.

“And what I fear here is that the absolutist and extreme position that you’re articulating would require us to go back on the promise of respect for religious believers,” Kavanaugh said.

But Stanford law professor Jeffrey L. Fisher said the decision can’t come down to “judicial assessments of whether religious views are honorable or offensive.”

He said if CSS’s view of being entitled to an exemption prevailed, “police officers could decline on religious grounds to enforce particular laws, prison guards could insist on evangelizing to inmates.”

Some justices, including Barrett, asked whether religious agencies could exclude mixed-race couples if that was a fundamental tenet of their beliefs.

Mooppan said no. The Supreme Court’s precedents make clear that “race is unique in this country’s constitutional history, and eradicating that type of racial discrimination . . . presents a particularly unique and compelling interest,” he said.

The court’s conservative majority has been increasingly open to claims that religious organizations deserve exemptions from some laws that would force them to compromise their faith.

Perhaps for that reason, liberal Justices Stephen G. Breyer and Sonia Sotomayor requested ideas for compromise.

If the agency’s objection “is to being required to evaluate and provide written endorsements of a same-sex relationship,” Breyer said, it could “add something onto any response you make and say that you do not endorse same-sex marriages. Say it.”

Fisher said that could be a place to start when Sotomayor asked him for a suggestion that “wouldn’t do real damage to all the various lines of law that have been implicated here.”

The city’s action had been upheld by lower courts under a precedent written by Justice Antonin Scalia called Employment Division v. Smith. It said that an individual’s religious beliefs did not excuse them from complying with an otherwise neutral and generally applicable law.

Catholic Social Services asked the court to overturn that precedent. But the Justice Department said the court could rule in the agency’s favor without disturbing the ruling, and there was little discussion of whether it should be junked.

The case is Fulton v. City of Philadelphia.