Jan. 4, 2013  -- A California appeals court overturned the rape conviction of a man accused of pretending to be a woman's boyfriend when he snuck into her bedroom and had sex with her, concluding that the law doesn't protect unmarried women in such cases.

Citing an obscure state law from 1872, the panel ruled that an impersonator who tricks someone into having sex with him can only be found guilty of rape if he is pretending to be a married woman's husband.

"Has the man committed rape? Because of historical anomalies in the law and the statutory definition of rape, the answer is no, even though, if the woman had been married and the man had impersonated her husband, the answer would be yes," Judge Thomas L. Willhite Jr. wrote in the court's decision.

ANOTHER INTERESTING READ: Accused Rapist's Defense: 'I Was Sleepwalking'

The court found that the woman consented, even though she was tricked. Had she been married the law defines such a deception as rape, but as an unmarried woman it does not.

The court ruled unanimously, if reluctantly, and sent the case back for retrial.

"We reluctantly hold that a person who accomplishes sexual intercourse by impersonating someone other than a married victim's spouse is not guilty of the crime of rape of an unconscious person," Willhite wrote.

Julio Morales was accused of raping an 18-year-old woman in February 2009, after the woman, Morales, and her boyfriend attended a party together where they were drinking. After coming home together Morales allegedly snuck into the woman's room while she was sleeping and had sex with her.

It was not until a beam of light from a window illuminated his face, that the woman realized she was not having sex with her boyfriend, but with Morales, according to prosecutors.

He was sentenced to three years in a state prison.

The three justices found that prosecutors advanced two theories at the time of trial. One was the impersonator theory, which the appeals court threw out. But prosecutors also suggested Morales raped a sleeping woman. When the case is retried, prosecutors will have to rely only on that theory, the court found.

The panel implied that their hands were tied given the law and implored California lawmakers to "correct the incongruity that exists when a man may commit rape … when impersonating a husband, but not when impersonating a boyfriend."

Justice Willhite said the California law had not been consistently applied. A similar law led to an acquittal in Iowa in 2010. That state's legislature amended the law to cover all women last year.

Reached by ABCNews.com, Morales' lawyer Ed Schulman declined to comment.

Calls to the California Attorney General's Office were not immediately returned.

The Associated Press contributed to this report.