Comedian Bill Cosby leaves the Montgomery County courthouse after pre-trial hearings in the sexual assault case against him in Norristown, Pa., Feb. 3, 2016. (Kena Betancur/AFP/Getty Images)

A sexual assault case against comedian Bill Cosby can proceed, a judge ruled Wednesday, clearing the way for prosecutors to try the embattled comedian over allegations a dozen years old.

Montgomery County Common Pleas Judge Steven T. O’Neill rejected defense arguments that Cosby had secured an immunity agreement in 2005 from the former county prosecutor that the case would never be reopened.

At the heart of the felony aggravated assault charge against Cosby is the alleged 2004 drugging and molestation of a former Temple University athletics official at the comedian’s suburban Philadelphia estate. The criminal case here is the first brought against the 78-year-old comedian, who has been accused of assault by more than 50 women, many of whom have filed civil lawsuits and claim they were also drugged.

“A secret agreement that permits a wealthy defendant to buy his way out of a criminal case isn’t right,” argued District Attorney Kevin Steele.

Cosby’s attorneys said the non-prosecution agreement was reflected in a news release issued by former district attorney Bruce L. Castor Jr. and was buttressed by an oral agreement that the DA made with Cosby’s then-criminal defense attorney, who has since died.

Castor, a polarizing and loquacious figure in local politics, took the witness stand for seven hours Tuesday, in part explaining why he didn’t press charges against Cosby in 2005. Although Castor believed the accuser, Andrea Constand, he noted several weaknesses in the case, including that Constand did not bring forth her allegations for a year. (Steele defeated Castor in an election last November, and the reopening the Cosby case was a central issue in the race.)

Castor said he believed that Constand’s allegations of assault would be better addressed in civil court. Ultimately, Constand filed a civil lawsuit that she and Cosby settled privately — but not before Cosby had to answer questions about, among other things, providing powerful drugs to women he wanted to have sex with. Those admissions, made in depositions, caused a firestorm when they were revealed last year.

John P. Schmitt, Cosby’s longtime personal attorney, said he would never have allowed Cosby to be deposed in the civil suit if the prosecutor’s oral agreement were not binding.

“A promise of a prosecutor, even an oral promise, is one that is absolute, 100 percent enforceable,” argued Cosby lawyer Christopher Tayback.

Cosby was charged in December, days before the 12-year statute of felony limitations expired. He has yet to enter a plea. A preliminary evidentiary hearing has been set for March 8.