New York CNN  — 

A federal judge ordered Michael Cohen to be released into home confinement after finding the government had sent him back to prison in retaliation for a tell-book he was writing about President Donald Trump.

During a phone hearing on Thursday, Judge Alvin Hellerstein rejected prosecutors’ argument that they didn’t customize a release form prohibiting Cohen from engaging with the media. Hellerstein said there was no other “inference” to take than retaliation for why Cohen was brought back into federal custody after he had been released on furlough.

“The purpose of transferring Mr. Cohen from furlough and home confinement to jail is retaliatory, and it’s retaliatory because of his desire to exercise his First Amendment rights to publish a book and discuss anything about the book or anything else he wants on social media,” Hellerstein ruled.

“In 21 years of being a judge and sentencing people and looking at terms and conditions of supervised release, I’ve never seen such a clause,” Hellerstein said.

Following the ruling, the Bureau of Prisons issued a statement saying, in part, “Any assertion that the decision to remand Michael Cohen to prison was a retaliatory action is patently false.”

Cohen sued the Department of Justice on Monday, alleging he was taken back into custody on July 9 to block the release of his book, which is slated for a September release, two months before the presidential election.

The judge ordered that Cohen be released by 2 p.m. Friday to serve the remainder of his three-year sentence in home confinement. Cohen will also be tested for coronavirus before he leaves the prison.

As part of the deal Cohen agreed to several other conditions of release that he had initially balked at, including that he seek approval for any possible employment, electronic surveillance and a requirement that family members would have to run errands and go grocery shopping for him.

While finding in favor of Cohen, the judge, however, said he would require some conditions as it relates to Cohen’s dealings with the media.

“Just as you wouldn’t have a press conference from a jail cell, you shouldn’t be able to have a press conference from your home. You can communicate, you can discuss, you can post on social media,” Hellerstein said.

“You can’t make a person confined in jail or at home into a totally free person. There’s got to be a limit,” he added.

The judge gave Cohen’s lawyers and prosecutors a week to negotiate the terms of his contact with the media.

Danya Perry, an attorney for Cohen, called the decision a “victory for the First Amendment.” She added, “This principle transcends politics and we are gratified that the rule of law prevails.”

The ruling is the latest twist in Cohen’s legal drama. He pleaded guilty in 2018 to tax fraud, lying to Congress and campaign finance violations for facilitating hush money payments to two women who alleged past affairs with the President. Trump has denied having affairs with the women.

Cohen was released to furlough in late May as part of a release program undertaken by the Bureau of Prisons to address the coronavirus pandemic.

A photographer from the New York Post captured Cohen dining at a restaurant near his apartment with his wife and another couple in early July. Cohen said that didn’t violate the terms of his release.

The following week Cohen reported to the courthouse to be transferred to home confinement and fitted with an electronic monitoring device. Cohen objected to the many of the conditions and was then taken into custody.

Prosecutors said Cohen was remanded because he had become combative and unwilling to agree to the terms. They told the judge that the probation officer and Bureau of Prisons official who authorized the form did not know Cohen was writing a book.

The judge rejected their argument, saying Cohen had been released on furlough without any conditions but that changed after he was back in the news.

“All of the sudden when the New York Post article comes out and the BOP understands that Cohen is writing a book and will likely finish before election time, he is imposed with conditions,” the judge said.

This story has been updated with additional developments.

CNN’s David Shortell contributed to this report.