A former Virginia mayor has pleaded guilty to distributing methamphetamine after getting caught in a drugs-for-sex bust last summer.

Ex-Fairfax Mayor Scott Silverthorne, 51, entered the plea Monday in Fairfax County Circuit Court.

Silverthorne had been free on bond from the time of his August 2016 arrest, but a judge ordered him held in jail until his June 9 sentencing.

Scroll down for video

Disgraced mayor: Former Fairfax Mayor Scott Silverthorne, 51, has pleaded guilty to distributing methamphetamine after being caught in a sting at a Virginia hotel last August

Bust: Silverthorne was arrested after offering an undercover detective 2 grams of crystal meth at the Crowne Plaza Hotel in Tysons Corner (pictured)

Attorney Brian Drummond says Silverthorne faces up to 40 years in prison and a fine of up to $500,000, but will likely get a light sentence or probation, considering that he does not have a past criminal record.

'The facts were such that it would have been futile to go to trial,' Drummond said. 'He is guilty of the transaction, but he didn’t get any money for it.'

The judge's order sending Silverthorne to jail pending his sentencing stunned his family and friends gathered in the courtroom, some of whom wept.

'I love him. He made a mistake,' the defendant's sister-in-law Mary Silverthorne told The Washington Post. 'Who is going to throw that stone? I know he’s learned from his mistake.'

Silverthorne was arrested on August 4 on a felony drug distribution charge after a sting at the Crowne Plaza Hotel in Tysons Corner.

Dirty secret: Police set up the sting after receiving a tip that Silverthorne was using a dating website called Bare Back Real Time to set up sexual trysts with men in exchange for drugs

Police said they set up the sting after receiving a tip that Silverthorne was using a dating website called Bare Back Real Time to set up sexual encounters with men in exchange for drugs.

They lured Silverthorne, who went by the user name 'Another Fun D.C. Bottom,' to the Tysons Corner location by posting a profile on the website that they said they expected would appeal to him, reported Inside NoVa.

Within two days, police said, Silverthorne, who was third-term mayor, contacted the undercover officers through the website to set up a meeting.

According to investigators, the mayor gave an undercover detective 2 grams of methamphetamine outside the hotel.

Following his arrest, Silverthorne was also fired from his job as a substitute teacher in Fairfax County Public Schools.

Terrible year: Silverthorne had lost his job, went bankrupt, lost his home to foreclosure and was diagnosed with cancer

Silverthorne had been re-elected mayor in May 2016 after a rough year in which he lost his job with the National Association of Manufacturers, filed for bankruptcy, lost his home to foreclosure and was diagnosed with cancer, The Washington Post reported.

In November, Silverthorne announced that he'd been diagnosed with squamous cell carcinoma, and the Post said he underwent treatment that ended two months before the election.

His father, the late Frederick Silverthorne, was mayor of Fairfax in the late 1970s and early 1980s.