Five months before Mark Vail’s expected release date from prison, an appeals court granted him a new trial in the case that put him away for the last seven years.

Vail, 35, an ex-teacher and coach from Charleston who was convicted and sentenced to 10 years in prison on a charge of having sex with a 12-year-old girl, could get another shot in court.

On Wednesday, the South Carolina Court of Appeals granted Vail a new trial, reversing a decision by a post-conviction relief court that denied him a new trial in 2011.

Vail’s attorneys argued that during his trial in 2005 in Charleston County, his attorney at the time did not object to what they called hearsay testimony by the victim’s family members about sexual encounters the victim had described.

The S.C. Court of Appeals opinion states that Vail’s then-attorney’s lack of objection to certain statements made by those witnesses was “highly prejudicial ... and affected the outcome of the trial.”

One of Vail’s attorneys, Travis Dayhuff of Columbia, said they were pleased with the order.

The S.C. Attorney General’s Office has 30 days to appeal that order for a new trial. Spokesman Mark Powell said whether or not they will appeal is under consideration.

Vail is serving his sentence in the S.C. Department of Corrections. He is expected to be released on July 13.

Vail, a former First Baptist Church School teacher and coach, declared innocence before and after a jury found him guilty of second-degree criminal sexual conduct with a minor and lewd act on a minor.

At the end of his trial, before security officers led him away, Vail shouted “I did nothing wrong.”

Vail was accused of having sex with a 12-year-old student at his James Island home beginning in 2003. During the trial, Vail’s attorney argued that the girl’s story was a fabrication.

He was arrested in January 2004 after the girl’s parents saw calls in her cellphone records. During the trial, the victim testified that the relationship was consensual. Prosecutors in the trial argued that it still amounted to unlawful sex with a minor.