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.

Judge Steven O'Neill issued the ruling after a 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.

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.

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

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.