Update, 6:40 p.m.: A federal judge ordered the government to release Pablo Villavicencio from his detention by Immigration and Customs Enforcement (ICE), and also blocked him from being deported—about six hours after hearing the case on Tuesday. Villavicencio was detained in June while delivering pizza to the Fort Hamilton military base in Brooklyn.

U.S. District Judge Paul Crotty agreed with Villavicencio's attorneys that he had the authority to rule on the case—because it started in New York—instead of sending it to a court in New Jersey. The government had argued for moving it across the river because Villavicencio is being held in the Hudson County jail.

"The rule of law, humanity and morality prevailed tonight with the Court's order releasing Pablo back to his family and community," said Adriene Holder, Attorney-In-Charge of the Civil Practice at The Legal Aid Society. "This decision should serve as a rebuke against the Trump Administration and its merciless crusade to tear families apart."

Crotty ruled that Villavicencio has the right to stay in the U.S. while his application for a green card is pending. His U.S. citizen wife sponsored him and they began the process in February.

Read Crotty's order here (PDF).
Earlier story: A federal judge in Manhattan heard arguments Tuesday on whether to release Pablo Villavicencio, the 35-year-old undocumented immigrant who was detained while delivering pizza to the Fort Hamilton military base in Brooklyn last month.

Although Villavicencio's lawyers are trying to get him freed, they spent much of their time focusing on why case should be heard in New York instead of in New Jersey, where he has been detained for more than 50 days.

Villavicencio is at the Hudson County Correctional facility, which is under a federal contract with Immigration and Customs Enforcement (ICE). The government's attorney, Joseph Cordaro, told U.S. District Judge Paul Crotty that a New Jersey Court would be the proper venue for the case because that's where Villavicencio is being detained.

Listen to Beth Fertig's afternoon report on WNYC:

However, Villavicencio's counsel countered that the original decision to detain him was made by ICE officials in New York City, and that the Hudson County jail cannot release him without approval from ICE.

Villavicencio came to the U.S. a decade ago from Ecuador. An immigration court gave him three months to leave the country voluntarily in 2010, but he stayed and is now considered deportable.

This information surfaced when he was stopped by a guard at the Fort Hamilton military base in Brooklyn while delivering pizza on June 1st. His job had taken him there many times before. But on that night his New York City municipal ID card was questioned and a guard conducted a background check. He was then turned over to ICE when the deportation order was discovered.

The incident stunned immigrant advocates, who see this case—and a similar detention involving a Brooklyn couple visiting a military base upstate on July 4th (just released after three weeks in detention)—as a sign that undocumented immigrants are increasingly vulnerable in places where they previously felt safe. There have been several rallies for Villavicencio including one in front of the federal courthouse Tuesday morning.

During the court hearing, Villavicencio's attorneys argued that the government also violated his right to due process by detaining him while he is still in the process of applying for a green card. Villavicencio's wife is a naturalized U.S. citizen and they started the application process together in February.

The couple's two young daughters sat with their mother, Sandra Chica, in the front row of the courtroom. Both wore dresses and pink flowers in their hair and one played with stuffed animals. During family visits to the jail where Villavicencio is being detained, Chica reportedly tells their daughters that they're visiting their father at work.

Villavicenio's wife and daughters leave the courthouse (Beth Fertig / WNYC)

Villavicencio's attorneys, Ann Marie Domyancic and Matthew Forbes of the firm Debevoise and Plimpton (which is working with the Legal Aid Society), explained how a 2016 decision by the Department of Homeland Security allows undocumented immigrants such as Villavicencio to apply for legal permanent residency without having to leave the U.S.

At one point, the attorneys seemed unprepared when Judge Crotty asked why Villavicencio had not applied for a green card before February of this year. But they argued that he is legally entitled to continue the process, and that the government blocked that by detaining him and seeking to deport him. He has an interview scheduled in August.

But Cordaro argued that Villavicencio could continue his application even if he is deported to Ecuador. This didn't appear to satisfy the judge, who questioned why the government was even trying to deport a man with no criminal record.

"The powerful do what they want and the poor are suffering what they must," Judge Crotty mused. Later he added, "What is the danger to the community for a man who has not committed a crime?"

Crotty did not issue a decision today, and did not indicate when a ruling could be expected.

Afterwards, one of Villavicencio's Legal Aid lawyers, Gregory Copeland, said he found the judge's remarks "reassuring."

"It doesn't sort of indicate which way he's going to roll, but I think at least the judge saw that a person that's not a danger and not a flight risk is now being detained for 52 days."

If the case does get moved to New Jersey, he said Villavicencio's team would make the same arguments for his release. But it would take longer to get another court hearing.

"This case is about a family that was torn apart and I think that having that family reunited as soon as possible is what matters here," he said.

Beth Fertig is a senior reporter covering courts and legal affairs at WNYC. You can follow her on Twitter at @bethfertig.