Some states punish gay couples for their bedroom activities. The Supreme Court, in a landmark gay rights case, is trying to decide when such laws are constitutional.

The court ruled 17 years ago that states could prosecute homosexuals who have sex. But in a case heard Wednesday, the justices appeared deeply divided on whether to reverse course and strike down a Texas law that bars "deviate sexual intercourse" by same-sex couples.

They framed the argument in moral and historical terms as a clash over equality, privacy and government's role in upholding traditional values.

"These are laws dealing with public morality, they've always been on the books and no one has ever thought they are unconstitutional simply because there are moral perceptions behind them," Justice Antonin Scalia said.

Other justices seemed less sure.

"The hard question here is, can the state pass anything it wants because the state thinks it's immoral? If you're going to draw a line anywhere, it might start with a line at the bedroom door," Justice Stephen Breyer said.

Texas said the law promotes the institutions of marriage and family. "Texas can set bright-line moral standards for its people," Houston District Attorney Charles Rosenthal said.

He was asked repeatedly if the state has evidence that there is some harm in sex between gays. He compared it to drug use.

"I don't see the parallel between the two situations," Justice David H. Souter said.

Laws forbidding homosexual sex, once universal, now are rare.

"Most Americans would be shocked to find out that the decision to engage in sexual relations with another person might result in a knock at the door and a prosecution," said Paul Smith, the attorney for two men who were arrested while having sex and prosecuted.

As recently as 1960, every state had an anti-sodomy law. In 37 states, the statutes have been repealed by lawmakers or blocked by state courts.

Of the 13 states with sodomy laws, four -- Texas, Kansas, Oklahoma and Missouri -- prohibit oral and anal sex between same-sex couples. The other nine ban consensual sodomy for everyone: Alabama, Florida, Idaho, Louisiana, Mississippi, North Carolina, South Carolina, Utah and Virginia.

"It would be legal in Texas to have sex with an animal, but not your long-term partner," said Dale Carpenter, a University of Minnesota law professor who filed a brief in the case.

A decision is expected before July.

The Supreme Court was widely criticized 17 years ago when it upheld an antisodomy law similar to Texas'. The ruling became a touchstone for gay activists.

Of the nine justices who ruled on the 1986 case, only three remain on the court. Chief Justice William H. Rehnquist was in the majority in the case, Bowers v. Hardwick, as was Justice Sandra Day O'Connor. Justice John Paul Stevens dissented.

The latest case drew an overflow crowd of lawyers, gay rights activists and spectators. Outside the court, a knot of protesters carried placards that read, "AIDS is God's Revenge," and other messages.

The two men at the heart of the case, John Geddes Lawrence and Tyron Garner, have retreated from public view. They were each fined $200 and spent a night in jail for the misdemeanor sex charge in 1998.

The case began when a neighbor with a grudge faked a distress call to police, telling them that a man was "going crazy" in Lawrence's apartment. Police went to the apartment, pushed open the door and found the two men having anal sex.

The case is Lawrence v. Texas, 02-102.