A convicted sex offender was attacked inside a New Hampshire courtroom Thursday as he sat down after being sentenced for sexually assaulting a 4-year-old girl.

Christopher M. Elwell’s attacker was not a parent or family member of the girl, however. His attacker was a fellow inmate.

Video shot by WMUR News 9 shows Elwell, 29, sitting down on a bench at the back of the Dover courtroom following his sentencing hearing, during which he was sentenced to 7½ to 15 years in prison. As soon as Elwell sits, a shackled inmate next to him stands and starts head-butting Elwell.

The other inmate, Josiah Davies, was awaiting arraignment and a probation violation hearing, WMUR said.

The video shows a deputy grab Davies and shove him away from Elwell, who appears to shake off the attack.

Assistant Strafford County Attorney Emily Conant told the news station that the two men did not appear to know one another. She said details of Elwell's case apparently upset Davies.

"They described him showing them videos of sexual acts similar to the ones that (the victim) had done," Conant said.

Elwell initially denied the allegations against him, which stemmed from an alleged sexual assault that took place in June at a Dover apartment where Elwell sometimes stayed. Investigators said the victim was one of three young children who were at the home when the assault occurred.

NH1, which obtained a copy of the arrest affidavit in the case, reported that Elwell's own 9-year-old daughter was among the children and witnessed the assault. 

The New Hampshire Union Leader reported that the children were participating in a sleepover that night. The girl's mother called police the following morning when her daughter told her what Elwell had done to her during the sleepover.

The defendant admitted that the accusations were true in court Thursday, WMUR reportedAccording to the Union Leader, Elwell admitted to police the day he was arrested that he watched pornography on his cellphone while in a bedroom with the three children.

He claimed that the 4-year-old wanted to imitate what she saw on the video, the newspaper reported.

He also pleaded guilty Thursday to failing to register as a sex offender prior to the assault. His sex offender status was the result of a similar conviction in 2008.

According to New Hampshire Department of Public Safety records, Elwell was convicted in September 2008 of two counts of felonious sexual assault of a 14-year-old. As of March 2016, he was registered as living at a home in Winchester.

At the time of his June arrest, he was living in Somersworth, the Union Leader said.

Conant told WMUR that Elwell was living intermittently at the home where the assault took place, but had not changed his registration to reflect the move.

In court, Conant said that the victim’s mother would have liked to see Elwell receive a longer sentence. His punishment was part of a plea deal designed to save the girl from having to recall the assault in court.

When Elwell is released from prison, he will not be allowed unsupervised contact with minors, WMUR reported.