By Nate Raymond

NEW YORK (Reuters) - A U.S. judge on Monday set a July trial date for an Albanian man who had been allowed to withdraw his guilty plea to a federal terrorism charge and mounted an unsuccessful challenge to the government's warrantless wiretapping program.

Agron Hasbajrami, a resident of Brooklyn, New York, had pleaded guilty in 2012 to attempting to provide material support to terrorists by sending money to a Pakistani militant group.

In October, Hasbajrami was allowed to withdraw his plea after the government disclosed it had monitored his communications without a warrant.

The surveillance was authorized under a 2008 law that allows U.S. agencies to collect millions of communications between Americans and foreign citizens abroad who are intelligence targets.

Hasbajrami subsequently moved to have the fruits of the surveillance suppressed. But U.S. District Judge John Gleeson in Brooklyn last month denied Hasbajrami's request.

At the time, Gleeson directed prosecutors to tell him whether they would offer Hasbajrami a new plea deal and if the agreement could include a carve-out allowing him to appeal the surveillance ruling.

At a court hearing Monday, prosecutor Seth DuCharme said he was "reluctantly optimistic" such a deal could be reached, and Steve Zissou, Hasbajrami's lawyer, said discussions were underway.

But after DuCharme said prosecutors were prepared to try the case if no deal emerges, Gleeson set a July 13 trial date.

The Justice Department's disclosure that the government had collected Hasbajrami's communications without a warrant came in light of a new policy put in place following disclosures by former National Security Agency contractor Edward Snowden.

Hasbajrami, 31, was one of five defendants to receive such a notice. He had previously been sentenced in 2013 to 15 years in prison.

Lawyers for Hasbajrami, as well as the American Civil Liberties Union and the Electronic Frontier Foundation, had argued the government should be required to obtain warrants to inspect communications from Americans swept up as part of the program.

The Justice Department countered that the program is already subject to oversight by Congress and by the Foreign Intelligence Surveillance Court, which approves requests to monitor foreign targets.

(Reporting by Nate Raymond; Editing by Noeleen Walder and Lisa Shumaker)