This is an archived article and the information in the article may be outdated. Please look at the time stamp on the story to see when it was last updated.

NORRISTOWN, Pa. — A judge refused to throw out the sexual assault case against Bill Cosby on Wednesday, sweeping aside claims that a previous district attorney had granted the comedian immunity from prosecution a decade ago.

Common Pleas Judge Steven O’Neill issued the ruling after a hard-fought two-day hearing.

The case now moves to a preliminary hearing to determine whether there is enough evidence to try the 78-year-old Cosby on charges he drugged and violated former Temple University athletic department employee Andrea Constand at his suburban Philadelphia home in 2004. The TV star could get up to 10 years in prison if convicted.

In 2005, then-District Attorney Bruce Castor decided the case was too flawed to prosecute. But Castor’s successors reopened the investigation last year after Cosby’s lurid, decade-old testimony from Constand’s civil suit was unsealed at the request of The Associated Press and after dozens of other women came forward with similar accusations that destroyed Cosby’s nice-guy image as America’s Dad.

At the hearing this week, Cosby’s lawyers tried to get the case thrown out by putting Castor himself on the stand. Castor testified that in deciding not to charge Cosby, he intended to forever close the door on prosecuting the comedian. He said he considered his decision binding on his successors.

Similarly, Cosby’s lawyers said they never would have let the TV star testify in the civil case if they didn’t believe criminal charges were off the table.

“In this case, the prosecution should be stopped in its tracks,” Cosby lawyer Chris Tayback argued. “Really what we’re talking about here is honoring a commitment.”

But current District Attorney Kevin Steele questioned whether Castor ever made such an agreement, since it was never put in writing on a legal document and the Cosby attorney with whom Castor dealt is now dead. Steele argued that in any case, Castor had no legal authority to make such a deal.

“A secret agreement that allows a wealthy defendant to buy his way out of a criminal case isn’t right,” Steele told the judge.

On the stand, Castor defended his decision not to bring charges, citing among other things Constand’s yearlong delay in reporting the allegations, her continued contact with Cosby, and suggestions that she and her mother might have tried to extort the comic.

The former DA said he made the no-prosecution commitment in hopes of prodding Cosby to testify in Constand’s lawsuit without invoking his Fifth Amendment right against self-incrimination. In the end, Cosby testified, denying he assaulted Constand but admitting among other things that he obtained quaaludes to give to women he wanted to seduce, and Constand eventually settled for an undisclosed amount.

As the case goes forward, Cosby’s lawyers are expected to fight mightily to keep the deposition from being introduced at trial.

Castor tried to make a comeback as DA last fall but lost to Steele in a hotly contested race, during which Steele ran ads criticizing Castor for not prosecuting Cosby when he had the chance. The tension between the two men was on display Wednesday when Steele accused Castor and the Cosby camp of engaging in “revisionist history.”

Most of the back-and-forth in court hinged on the wording and interpretation of a 2005 press release in which Castor announced he would not prosecute Cosby. Castor found himself sparring with prosecutors over many seemingly inconsistent statements he made over the years on whether Cosby could still be charged.

The judge said he struggled to find similar cases where a suspect who was never charged received a promise that he would never be prosecuted. Normally, immunity is granted after a suspect is charged because he or she can provide testimony or information to prosecutors.

While more than 50 women have accused Cosby of drugging and sexually assaulting them since the 1960s, the statute of limitations for prosecuting the comic has run out in nearly every instance. This is the only case in which he has been charged.