Jose "Ivan" Noe Nuñez Martinez, a gay Mexican immigrant taken into custody in January as he met with federal authorities in West Philadelphia to try to resolve his status, walked out of a detention center Wednesday night after winning the opportunity to post bail.

Nuñez Martinez left the York prison soon after the $10,000 bail was paid, according to his lawyer, Audrey Allen. He was quickly reunited with his husband, Paul Frame.

"He's thrilled," Allen said soon after the decision. "He got bond."

Nuñez Martinez faces obstacles to remaining in the United States permanently, though lawyers say it is always better to fight an immigration case while not in detention.

His many supporters in the Philadelphia region have insisted that he deserved to be free to pursue his filings. He represents no threat to the community, they say, and is unlikely to flee, given that his husband, an American citizen, lives in the area.

"This can be fairly celebrated as a victory," said ACLU lawyer Golnaz Fakhimi, who also worked on Nuñez Martinez's behalf, joining lawyers including Ilana Eisenstein and Ben Fabens-Lassen of DLA Piper.

Immigration and Customs Enforcement officials have said that Nuñez Martinez was unlawfully present in this country and had previously been removed. Undocumented immigrants who reenter the U.S. after being deported can be charged with a felony offense, although Nuñez Martinez was never charged.

Nuñez Martinez, 37, a native of Michoacán, Mexico, fled to the U.S. after the murder of a gay friend in 2001. He said he feared that he, too, would be killed if he returned. He eventually settled in Chester County, where he worked for an auto-body repair shop.

In August 2014, he met and fell in love with Frame, an American citizen, and they married in April 2016.

This year, on Jan. 31, the couple arrived at what they expected to be a routine interview with officials of U.S. Citizenship and Immigration Services in West Philadelphia. Instead, Frame and others said, ICE officers entered the room, put Nuñez Martinez in handcuffs, and took him away.

"What Ivan and his family have had to endure the last 10 months is completely unacceptable," said Francisco Cortes, interim director of Galaei, the Philadelphia agency that describes itself as a queer Latin social-justice group. "In a time when immigrant communities and LGBTQ people are constantly under attack, we must come together to support, protect, and center those living at the margins."

Frame earlier said that his husband was doing exactly what immigration critics demand, trying to "get in line" and file the proper papers to live here legally.

In May, attorneys filed a federal habeas corpus petition on Nuñez Martinez's behalf, seeking a bond hearing. Last week, U.S. District Judge Christopher Conner ordered the immigration court to hold that hearing.

On Wednesday, Nuñez Martinez appeared before Judge Kuyomars Golparvar at the York Immigration Court and was told he could post bail and leave.

"Ivan and I have missed celebrating our birthdays, holidays, and our anniversary while he has been behind prison walls," Frame said before the decision. "The stress we have both been under has been unending. All we want is to be able to live as a married couple again."

Nuñez Martinez's case is complicated by a circumstance that can often bar immigrants from ever being allowed to legally remain in the U.S.

In 2010, he returned to Mexico to see a sick family member. When he attempted to reenter the U.S., he was stopped at the border, briefly detained, then quickly sent back to Mexico.

That occurs so often at the border that many people who are returned to Mexico don't even realize that the U.S. government may consider them to have been deported. Many attempt to return within a day or so, as did Nuñez Martinez, this time successfully.

But reentering after deportation is considered a serious offense by immigration officials.

In the U.S., his advocates said, Nuñez Martinez lived a peaceful and productive life, working for a janitorial-service company as well as the auto repair shop.