A high-risk, 18-year-old sex offender in North Carolina may soon be a free man — because he is an illegal immigrant and the state has determined it cannot pay to put him in a treatment facility.

The Guatemalan man, identified only as "D.G," was convicted of raping his 5-year-old handicapped cousin when he was 15. Despite recommendations by criminal psychologists that he be sent to a sex-offender treatment program, he wound up in a youth detention center instead.

But D.G. can stay in the center only until his 19th birthday — and with that day approaching, what happens next is anyone's guess.

D.G.’s attorney, Ann Petersen, said the case didn't always seem so complicated.

"When he was originally adjudicated in 2005, the trial judge took into account all of the expert psychological examinations and his background and determined that there was a program that was well suited for him through a private agency," Petersen told FOXNews.com.

"He was sent off in September and doing great in the program until the county decided they didn’t want to pay for it."

In February of 2006, a motion for review was filed stating that "funds were no longer available" for D.G.'s placement. A notice from the local mental health agency, Foothills Area Program, soon followed, saying the $128,000-a-year services being provided to him would be terminated effective March 31, 2006.

"The court decided that it couldn’t force the state or county to pay for it because there’s this statute that says that for illegal aliens no government agency should be forced to pay a benefit," Petersen explained.

With no public funds available for D.G.’s treatment, the court moved him to a youth development center, where he could remain in custody only until he turned 19.

"It’s not a treatment center, it’s a jail," Petersen said of the facility. "He couldn’t get treatment for sexual deviance there, which is what he needed."

D.G. appealed the ruling, but a court determined that under federal law, he was not a "qualified alien" — and thus the state could not provide funding for him to receive "custodial sex offender treatment," court documents show.

On Feb. 23, Petersen will take D.G.'s case to the North Carolina Supreme Court and argue again that D.G. should be committed to treatment. If her appeal is unsuccessful, D.G. will be released, from the development center untreated.

But exactly when that will happen is unclear.

D.G. cannot be kept in the center past his 19th birthday, but Petersen said she doesn't know when his birthday is and, considering illegal immigrants often have faulty documents, it's possible even the state doesn't know for sure.

The even bigger mystery is where he will go when he is released.

The court has established that D.G. can’t return to his family's house in North Carolina, as his victim lives there. But beyond that, little is known about where he will end up.

Petersen said she doesn't know what will happen or whether the federal government will deport D.G. The district attorney, when contacted by FOXNews.com, declined to comment on the case.

An immigration attorney, Erena Baybik, is surprised D.G. isn’t already back in Guatemala.

"Since in most cases, a sex crime is deemed an aggravated felony under immigration law, the alien would be automatically deported because there is no defense to aggravated felonies under immigration law," Baybick told FOXNews.com. "I am a bit surprised at all of the bureaucratic hoops that the government is having to go through in North Carolina in order to find a resolution to this case."

But Cheryl David, the the national chairman of the American Immigration Lawyers Association, said deporting D.G. might not be that simple.

"Since it's a youthful offender adjudication, he might not even be deportable at all," she said.

Former Immigration and Naturalization Service Special Agent Michael Cutler said the idea of keeping D.G. in the country is deplorable.

"The bottom line is he committed a heinous crime and he poses a threat to people in the community where he lives, and I don't think the United States has a burden to keep mentally incompetent or criminally insane people in this country when they don't belong here," Cutler told FOXNews.com.

"He may need help, but there are children in America who aren't getting the proper education that they need to help them with a variety of issues either.... If you can't feed your own children, you don't invite more people to the dinner table."

Whether D.G. stays in the U.S. or is sent back to Guatemala, former federal prosecutor and immigration attorney Michael Wildes said he will still be a threat to children, and American money will have been wasted.

"This is a lose-lose for everybody, including taxpayers who are going to end up having to pay for care that isn’t going to change the defendant," Wildes, currently the mayor of Englewood, N.J., told FOXNews.com. "Simply put, an individual with this problem should be treated."

And at this point, that might be easier said than done.

"By pulling him out of treatment he was sent the message that he was wrong to trust the program and the providers, and this could have further damaged him," forensic psychiatrist Dr. Hans Stelmach told FOXNews.com. "It might be tough to re-engage him in the program."

But all that considered, Stelmach said, he would still recommend that D.G. return to a treatment program.

Petersen said even if help is out of reach for D.G., it's not out of reach for others.

"It’s kind of a peculiar case because the appellate process takes so long that by the time we get this resolved, unfortunately, it may or may not be too late for him," she said. "But hopefully his case will prevent this from happening to someone else."