California's supreme court has upheld a ban on gay marriage in the state after voters used a referendum to overturn an earlier ruling in favour of recognising homosexual weddings.

But the court ruled that the 18,000 gay marriages that have already taken place will continue to be recognised by the state government.

The ruling is the latest twist in a social battle that is increasingly replacing abortion as a rallying point for conservatives after several states legalised gay marriage in the past years, with more expected to follow in the coming months, while others have passed laws barring them.

The California judgement was met with cries of "shame on you" by angry gay rights activists outside the court who said they will not let the issue rest and will attempt to organise a second referendum to overturn the ban.

The original vote, known as Proposition 8, imposed a constitutional amendment defining marriage as a union "between a man and a woman" in the state's constitution. It passed in November with 52% of the ballots in favour.

The backlash against gay marriage came after a supreme court ruling in May that the state constitution's ban on discrimination did not permit a bar on homosexuals marrying. The court said that the unions were a "basic civil right" and "an individual's sexual orientation, like a person's race or gender, does not constitute a legitimate basis upon which to deny or withhold legal rights." The court also spoke of the need for gay couples to be treated with "respect and dignity". Marriages began in June but religious groups and conservatives swiftly began organising against them.

According to the Los Angeles Times, California has more than 100,000 households headed by gay couples. About one quarter of them have children.

The legal battle over gay marriage in California began five years ago when the mayor of San Francisco defied the law and began issuing marriage licences to same sex couples. That prompted a number of states, particularly in the south, to specifically ban gay marriages.

While the California supreme court struck down the marriages performed in San Francisco, it did open the way for an alternative legal path to their recognition and when the issue returned to the high court last year it upheld the right of gays to marry.

At the time, California became only the second state after Massachusetts to permit same-sex marriage. But since then four others have legalised in, including the usually conservative state of Iowa, and three others are considering legislation.

Shortly before the California supreme court ruled, Jennifer Pizer, a lawyer with the gay rights group Lambda Legal which represented one of the plaintiffs in the case, told the Washington Post that they will press for another referendum.

"All the legal and policy and political groups feel very strongly that the sensible course would be another ballot measure to undo Proposition 8," she said.

Gay rights groups and supporters of same-sex marriages were planning rallies across California after the ruling to protest against the ban.