Maria Puente, USA TODAY and Brittany Horn

USA TODAY Network

NORRISTOWN, Pa. — The criminal sexual-assault charges against Bill Cosby here will stand and will proceed to the next step before a trial, a judge ruled Wednesday, after a two-day hearing.

Montgomery County Judge Steven O'Neill rejected Cosby's motion to dismiss the charges of aggravated indecent assault stemming from a 2004 encounter with ex-Temple University employee Andrea Constand.

O'Neill ruled against Cosby's argument that he had an immunity deal with the then-district attorney not to prosecute in return for testifying fully in a deposition for a civil suit filed by Constand.

Cosby has also argued that current District Attorney Kevin Steele should be dismissed from the case on the grounds of bias and politics: Steele defeated former DA Bruce Castor in the district attorney race in November, and promised to pursue Cosby while criticizing Castor for not prosecuting him 11 years ago.

But O'Neill ruled against Cosby on that matter, too: Steele can continue prosecuting the case, as he found no professional misconduct.

A preliminary hearing in the case is scheduled for March 8, when the focus will be determining whether prosecutors have enough evidence to put Cosby on trial. Among other issues, Cosby will likely try to persuade the judge to rule his deposition testimony inadmissible.

Cosby held his head in his hands after the ruling, then left the courtroom, using a cane to descend the staircase. He waved and smiled at supporters but had no comment. His lawyers put their arms on him to comfort him, the Associated Press reported.

Cosby also has argued the charges should be dismissed because of the delay — 12 years — between the encounter and the charges filed in December by Steele. That issue may be taken up at the March hearing.

But the motion to dismiss based on the no-prosecution deal between Cosby and Castor was the most important issue, taking up two days of testimony in a hearing here in the courthouse outside Philadelphia.

Neither Cosby's lawyers nor Steele talked to the media, but the ruling by O'Neill was a clear victory for Steele, who argued that Castor had no authority to unilaterally grant immunity to Cosby in 2005, and that if there had been such an agreement, it had to be in writing and approved by a judge to be valid.

Thus, the testimony on Tuesday and Wednesday focused on this deal that Cosby said should shield him from prosecution now.

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

Cosby's lawyers argued that prosecutors can and do make no-prosecution deals with defendants, and those deals are binding on future prosecutors.

“This prosecution should be stopped in its tracks,” said Cosby lawyer Christopher Taybeck. "When a prosecutor gives his word, that’s something that is enforceable.”

Cosby’s general counsel, John Schmitt, testified that he and Cosby were satisfied with the press release about the agreement, written and signed by Castor in 2005, as the documentation they needed to show Cosby would never be prosecuted. Plus, Schmitt said, he received numerous “oral assurances” from Castor that no charges could ever be filed.

Without these, Schmitt said, he would never have let Cosby sit for a deposition for a civil suit if there was a potential threat of criminal prosecution for what he said.

Earlier, Cosby, again in a suit and tie, smiled and nodded at onlookers as security-assistants helped him walk up a ramp into the courthouse. Some supporters yelled, "We love you, Bill!" according to the Associated Press.

Judge O'Neill said Wednesday, after seven hours of testimony on Tuesday, that he expected to decide today on Cosby's motion to dismiss the felony charges stemming from an encounter with Constand at Cosby's home nearby in 2004.

Key witness to Bill Cosby: 'I'm not on your side'

Constand said Cosby drugged and raped her; Cosby said the encounter was consensual. Now, 12 years later, Steele hopes to use Cosby's own words about the encounter — from a recently released deposition in a civil suit Constand and Cosby settled in 2006 — to bolster his charges that Cosby committed a crime.

Before she filed her civil suit, Constand tried to pursue Cosby on criminal charges in 2005, but then-DA Castor declined to prosecute for lack of evidence.

According to his testimony Tuesday, Castor believed Constand had been molested by Cosby but he thought he couldn't prove it a year after the alleged crime. Also, he had doubts about Constand's credibility and thought her behavior in interviews with police was "inconsistent" with that of a rape victim, he said in testimony.

Dolores Troiani, Constand's lawyer, testified Wednesday, undercutting some of Castor's reasons for his actions and inactions regarding Cosby and Constand 10 years ago. She dismissed Castor's stated reasons for not prosecuting Cosby, suggesting his real reasons were political since he was running for office at the time. Troiani said Castor likely didn’t want to “alienate fans of Dr. Huxtable,” Cosby’s character in The Cosby Show.

Anyway, she said, she didn't need Castor to promise Cosby not to prosecute to get Cosby to sit for a deposition in the civil suit; if he refused, she said, she could ask a judge to compel him.

Cosby's other grounds for dismissing the charges involves the delay between the date of the encounter with Constand and the filing of criminal charges. Cosby argues it's too long and justifies dismissal.

In court papers, Steele counters that by noting that new evidence in the case — Cosby's own words in his deposition — did not become available until July 2015, when the Associated Press persuaded a judge to release portions of the long-sealed deposition.

That triggered a new investigation, as the statute of limitations deadline (at the end of January in Pennsylvania) loomed, Steele said. "This case appeared to be one of the few (accusations against Cosby) in which a prosecution might still be possible," Steele said in court papers. "The need to act, to seek justice, could not be ignored."

Maria Puente reported from McLean, Va., and Brittany Horn of The News Journal reported from Norristown, Pa.