California's Supreme Court expressed serious doubts Tuesday that city officials here had the right to issue marriage licenses to nearly 4,000 gay and lesbian couples earlier this year in defiance of state law.

During a two-hour hearing broadcast live across the state, the seven justices on the court repeatedly suggested that allowing San Francisco to ignore California's laws on marriage -- which prohibit same-sex unions -- could inspire municipalities to ignore other statutes ranging from gun control to gay rights.

Justice Joyce L. Kennard, echoing a point several justices raised during the hearing, said that such a step would create "no certainty" to state laws. "Wouldn't that be setting a problematic precedent?" she asked.

Justice Ming Chin noted that a bill legalizing gay marriage is pending in the state legislature. If passed, he said, "would the mayors throughout the state be free to disregard it?"

The high court convened Tuesday's hearing only to listen to rival arguments on the narrow issue of whether San Francisco Mayor Gavin Newsom overstepped his authority by sanctioning the marriages -- not the larger question of whether gay unions are constitutional. The justices also are considering whether to nullify more than 4,000 marriages if they decide that Newsom's actions were illegal. They are required to issue a ruling in the next 90 days.

After the hearing, some advocates of gay marriage said that it appears unlikely that the justices will uphold the Democratic mayor's actions, and might invalidate all of the marriages. But they said they still have confidence in their cause.

"I think it's obvious that the court has serious concerns and reservations about what the city did," Kate Kendell, the executive director of the National Center for Lesbian Rights, told reporters outside the courthouse. "But what happened today, and what happens when the court eventually rules on this issue, says nothing about their ultimate analysis on the key constitutional question."

Legal and political battles over gay marriage are intensifying across the country. Last week, Massachusetts began issuing marriage licenses to same-sex couples with the blessing of the state's highest court, which ruled last November that denying gays and lesbians the right to wed is unconstitutional. More than 1,000 couples have since tied the knot.

But the fight there also is just beginning. In March, the Massachusetts legislature gave preliminary approval to a proposed state constitutional amendment to ban gay marriages. It faces several hurdles before it can be placed on the ballot in 2006.

California's Supreme Court stopped San Francisco's month-long marathon of gay marriages in March at the request of the state's attorney general, Bill Lockyer (D), and a conservative group that opposes same-sex unions. Both said the city's decision violated state law, which defines marriage as the union of a man and woman, and a voter-backed ballot initiative in 2000 that reinforced that law.

Arguing for the state, Deputy Attorney General Timothy Muscat told the justices Tuesday that San Francisco trampled on the rights of courts and the legislature when it decided to marry gay couples.

"Here we had city officials in clear defiance of state laws not only not enforcing the law, but going a step further and rewriting the law," Muscat said, adding later, "They simply had no authority."

Jordan Lorence, an attorney for the Alliance Defense Fund, a conservative group based in Arizona, warned the court that upholding San Francisco's decision would wreak havoc on the state. "You're going to have a Balkanization of family law," he said, suggesting at one point that California municipalities would be free to legalize polygamy if the court upheld Newsom's actions.

Lorence also urged the court to void the marriages, saying gay couples wed by the city have always known that their marriages might not have legal standing. "This was essentially an act of disobedience," he said.

But Deputy City Attorney Therese Stewart adamantly defended Newsom's decision by saying that his first allegiance as a public official is to the state constitution, which bars discrimination.

Several justices challenged her by saying that Newsom could have turned to the courts to fight California's ban on gay marriage and that there was no urgent reason for him to act, particularly inasmuch as the state has a new law that expands the rights of gay couples who register as domestic partners.

"It is separate and not equal," Stewart replied.

She also argued that the weddings of the couples should remain valid at least until the court resolves the constitutionality of gay marriage. That question is now before lower courts in California and may take two years to reach the state Supreme Court.

Throughout the proceedings, several justices expressed empathy for the couples' uncertain legal status and expressed dismay that the city had placed them in such a position. At one point, Justice Marvin Baxter said that San Francisco had created "a mess."

Special correspondent Joe Dignan contributed to this report.

Attending a hearing by the California Supreme Court are, from left, John Lewis, Stuart Gaffney, Molly McKay and Beverly Senkowski. Lewis and Gaffney were wed in San Francisco in February.California Deputy Attorney General Timothy Muscat told justices that San Francisco disregarded the courts and the legislature.