California's Second District Court of Appeals yesterday granted a retrial for a man convicted of raping a woman by sneaking into her bed and pretending to be her boyfriend.

The reason? She wasn't married, according to LA Weekly, which posted the ruling Thursday.

"A man enters the dark bedroom of an unmarried woman after seeing her boyfriend leave late at night, and has sexual intercourse with the woman while pretending to be the boyfriend," the court said in its ruling (emphasis ours). "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."

The woman, identified only as Jane Doe, said she woke up in the middle of the night "to the sensation of having sex," which confused her because she and her boyfriend had agreed not to have sex before he left for the night. When she was able to get a glimpse of the man's face, she says, she saw it wasn't her boyfriend but a man named Julio Morales.

Jane says she screamed and tried to push Morales away, and he eventually left the room.

Morales admitted he had sex with Jane and said "he also thought she believed he was her boyfriend," according to the appeals court ruling.

However, Morales' defense team said he didn't remember Jane trying to push him away, and that he did not try and continue having sex after he initially pulled out of her.

In his appeal, which was decided Thursday, Morales claimed his conviction was faulty because Jane didn't think he was her husband, but rather just her boyfriend.

California's penal code says a crime is rape if the victim was unconscious or asleep or "submits under the belief that the person committing the act is the victim's spouse, and this belief is induced by any artifice, pretense, or concealment practiced by the accused, with intent to induce the belief."

However, since Jane and Victor were not married, Morales couldn't be accused of pretending to be her husband, which means he didn't violate California's penal code.

"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," the appeals court ruled.

Read the full ruling for yourself:

People v. Morales

The law cited by the court was written in 1872 and doesn't offer unmarried women the same protections against rape as it does married women, The Daily Mail reported Friday morning.

Morales has already served his three-year sentence and will be getting a new trial, according to LAWeekly. A key issue in the new trial will be whether the alleged victim was asleep when Morales had sex with her, in which case he could be convicted of rape.

DON'T MISS: Teen Girls Accused Of Drugging Parents' Milkshakes Because They Were Too Strict >