California appellate judges urged legislators to update an arcane 19th century law, as the panel reversed the rape conviction of a man who authorities say pretended to be a sleeping woman's boyfriend before initiating intercourse.

The Los Angeles-based appeals court said that the 1872 measure doesn't give single women the same protections as their married counterparts in certain rape cases.

Julio Morales had been convicted and sentenced to three years in state prison, found guilty of entering a woman's bedroom late one night once her boyfriend had gone home and initiating sexual intercourse while she was asleep, after a night of drinking.

But a panel of judges overturned the trial court's conviction and remanded it for retrial, in a decision posted this week.

The victim said her boyfriend was in the room when she fell asleep, and they'd decided against having sex that night because he didn't have a condom and he had to be somewhere early the next day.

Morales pretended to be her boyfriend in the darkened room, and it wasn't until a ray of light from outside the room flashed across his face that she realized he wasn't her boyfriend, according to prosecutors.

"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.

The appeals court added that prosecutors argued two theories, and it was unclear if the jury convicted Morales because the defendant tricked the victim or because sex with a sleeping person is defined as rape by law.

The court said the case should be retried to ensure the jury's conviction is supported by the latter argument.

The decision also urges the Legislature to examine the law, which was first written in response to cases in England that concluded fraudulent impersonation to have sex wasn't rape because the victim would consent, even if they were being tricked into thinking the perpetrator was their husband.

Watch the most-viewed videos on NBCNews.com

Willhite noted that the law has been applied inconsistently over the years in California.

In 2010, a similar law in Idaho prevented an unmarried woman from pressing rape charges after being tricked into sex with a stranger by her then-boyfriend.

The judge called what happened "despicable" but said the state's law left the court with no choice. Idaho's law was amended to cover all women in 2011.

Morales' attorney Edward Schulman declined comment when reached by phone Thursday.

Prior to the conviction, Schulman had argued Morales believed the sex was consensual because the victim responded to his kisses and caresses, according to the decision.

More content from NBCNews.com:

Follow US news from NBCNews.com on Twitter and Facebook