A judge refused to throw out the sexual assault case against Bill Cosby, sweeping aside a former district attorney's claim that he 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, saying witness credibility was a factor.

In another setback for the defence, the judge also denied a request to disqualify newly elected District Attorney Kevin Steele from the case.

Cosby's lawyers had accused Mr Steele of making a "political football" out of the comedian during the campaign.

Cosby, 78, was arrested in December and charged with drugging and violating former Temple University athletic department employee Andrea Constand at his Philadelphia home in 2004.

The TV star could get up to 10 years in prison if convicted.

Cosby held his head in his hands after the ruling, then left the courtroom, using a cane to descend the ornate marble staircase. He waved and smiled at supporters but did not comment.

The next step is a preliminary hearing on March 8 to decide whether prosecutors have enough evidence to put him on trial.

The charges represented a U-turn by the district attorney's office.

In 2005, then-district attorney Bruce Castor decided the case was too flawed to prosecute.

But his successors reopened the investigation last year after Cosby's lurid, decade-old testimony from Ms 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.

Cosby's lawyers tried to get the case thrown out with help from Mr Castor, who testified at this week's hearing that 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 did not believe criminal charges were off the table.

"In this case, the prosecution should be stopped in its tracks," Cosby lawyer Chris Tayback said. "Really what we're talking about here is honouring a commitment."

Mr Steele challenged Mr Castor's credibility and questioned whether the former DA ever made such an agreement, since it was never put in writing on a legal document and the Cosby lawyer with whom Mr Castor dealt is now dead.

Mr Steele argued that in any case, Mr Castor had no legal authority to make such a deal, telling the judge: "A secret agreement that allows a wealthy defendant to buy his way out of a criminal case isn't right."

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.

As the case goes forward, Cosby's lawyers are expected to fight 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.

Sign up to our free newsletters

Get the best updates straight to your inbox:
Please select at least one mailing list.

You can unsubscribe at any time by clicking the link in the footer of our emails. We use Mailchimp as our marketing platform. By subscribing, you acknowledge that your information will be transferred to Mailchimp for processing.