Actor and comedian Bill Cosby arrives Thursday for a pretrial hearing for his sexual assault trial at the Montgomery County Courthouse in Norristown, Pa. (Dominick Reuter/Pool/Reuters)

NORRISTOWN, Pa. — In the fierce, months-long legal slugfest leading up to Bill Cosby’s retrial on sexual assault charges, the comedian’s high-powered defense team has repeatedly lost fights over evidence and witnesses.

Steven T. O’Neill, the veteran Montgomery County, Pa., judge overseeing the case, has already handed a huge victory to prosecutors by allowing five previous accusers to testify that they were allegedly drugged and sexually assaulted by Cosby.

On Thursday, just days before jury selection in the retrial is set to begin, the defense was thwarted again, failing to force O’Neill to remove himself from the case.

In an awkward hearing, Cosby’s lead attorney Thomas Mesereau argued O’Neill might be biased because the judge’s wife, Deborah V. O’Neill, works for a sexual trauma center at the University of Pennsylvania, where she is employed as a social worker.

“We are concerned that there might be an appearance of bias,” Mesereau told the judge whose motivations he was questioning.

O’Neill, though, his voice cracking with emotion, eventually rejected the defense request, relying on a legal technicality by saying it had not been filed in a timely manner but also firmly asserting he is not influenced by his wife’s work. He accused Cosby’s attorneys of “trivializing” his wife’s accomplishments by suggesting she had some sway over his judicial rulings. In a lengthy statement from the bench, O’Neill professed his “deep” love for his wife and his admiration for her achievements.

“She is an independent woman, and she has a right to be involved in anything she believes in,” O’Neill said. “It’s difficult to have her accomplishments trivialized.”

The scene played out in a courtroom thick with tension. O’Neill, who at times can be folksy and chatty, entered the courtroom in the morning with pursed lips. He fumbled with papers for several moments as a long and uncomfortable silence hung in the air.

O’Neill was clearly irked at the defense, and he did nothing to hide his annoyance at Cosby’s team for distributing their court filing demanding his recusal to the media before it was officially accepted by the clerk of court’s office. Cosby, dressed in a dark blue suit with a red tie and matching pocket handkerchief, watched the back-and-forth with a look of bemusement. As Mesereau and his co-counsels argued with the judge, Cosby — now 80 — periodically smirked, smiled and chuckled as he rocked in his swivel chair at the head of the defense table.

The courtroom machinations delved extensively into the work of the judge’s wife, plunging into her speaking engagements and into her academic career by addressing her academic writings about acquaintance rape. Until last week, when the defense filed paperwork asking for O’Neill’s recusal, the judge’s wife had played no public role in the case. The defense’s court filings, which include Deborah O’Neill’s photograph and email address, trained a spotlight on her.

The defense asserted that the judge’s wife has described herself as an “activist and advocate for sexual assault victims.” Cosby’s defense team noted Deborah O’Neill spoke at a University of Pennsylvania campus event in 2013 sponsored by a group called Abuse and Sexual Assault Prevention. Several years later, the same group called on the university to rescind an honorary degree it had awarded to Cosby. The degree was rescinded in February.

It was another defense allegation that became the main focus of Thursday’s squabble. The defense alleged that Deborah O’Neill made a donation to an activist group — Women Organized Against Rape — that plans to hold demonstrations during the Cosby retrial. The evidence the defense presented is an Internet donation page for the group that shows Deborah O’Neill’s name and photograph.

Judge O’Neill sternly said his wife’s employer made the donation and that none of their marital assets were involved. He said he did not know her name and face ended up on the page.

Prosecutors, who have had the upper hand in the battle over pretrial legal motions, pushed back against the request to remove the judge. They pointed out the donation was made more than a year ago — long before the activist group announced plans to hold a demonstration.

Mesereau, best known for winning an acquittal for pop star Michael Jackson on child molestation charges, held his ground.

“It still appears to us that your wife was responsible for the donation,” Mesereau told the judge.

O’Neill revealed Thursday that the possibility of bias related to his wife’s work was raised in the first trial. For the first time, he divulged that Cosby’s original defense attorney, who has since withdrawn from the case, told him in a phone conference Cosby wanted the judge to recuse himself after an Internet blogger argued he might be biased. However, the attorney, Brian McMonagle, chose not to make a formal request, O’Neill said, and the issue faded away.

O’Neill also took pains Thursday to distance his legal rulings from his wife’s professional work. Deputy District Attorney Robert Falin read a statement O’Neill had previously made about how his wife’s profession has no impact on his decision-making.

“This type of smoke machine tactic should not be tolerated,” Falin said.

In a previous setback for the defense, O’Neill resisted greenlighting the defense’s plan to call a witness who says Cosby’s main accuser, Andrea Constand, was willing to lie about being sexually assaulted by a celebrity to make money from a lawsuit.

Jury selection is scheduled to begin Monday, with the judge and the attorneys looking to seat 12 jurors who can decide whether Cosby drugged and sexually assaulted Constand at his suburban Philadelphia home in 2004. The first jury to hear the case could not reach a unanimous verdict in June 2017, and O’Neill declared a mistrial. Mindful that possible jurors in the retrial may very well be paying attention, O’Neill pleaded with both sides to watch what they say and do.

“Everything that you say,” he said, “the world is listening. You don’t need to do this labeling, you don’t need to do this name calling.”