By Krisztina Than

BUDAPEST (Reuters) - Hungary's parliament on Tuesday narrowly voted down a plan by Prime Minister Viktor Orban to reject EU quotas for accepting refugees, a setback for the maverick leader that could weaken him in a long-running fight with Brussels.

In a rare parliamentary defeat for Orban, his proposed constitutional amendment to ban the resettlement of migrants in Hungary won only 131 votes in the 199-seat parliament, just short of the necessary two-thirds majority of 133.

Orban's determination to keep out migrants and refugees, including by building razor-wire border fences, has angered his fellow European Union leaders and complicated their task as the EU struggles to cope with an influx of 1.4 million people since the start of 2015, many fleeing conflicts like the war in Syria.

The far-right Jobbik party sealed the bill's rejection by boycotting the vote. But it held out a lifeline to Orban by saying it would throw its support behind the ban if he scrapped a separate scheme allowing foreigners to buy residency rights.

Backing down to Jobbik would be politically difficult for the prime minister. Orban's right-wing Fidesz party said its leadership would meet to discuss its next move.

"As for Jobbik's ideas, we maintain what we said before: there is no room for political blackmail in case of a national issue," government spokesman Zoltan Kovacs said in an email to Reuters. "We will be able to talk about further steps in light of the decisions made by Fidesz's presidency."

"TEMPORARY FIASCO"

Orban had said the amendment was needed to honor an October referendum in which more than 3 million Hungarians, an overwhelming majority of those who voted, rejected EU quotas stipulating how many migrants member states must accept.

He said that even though the referendum was not legally binding because of low turnout, it gave him a strong political mandate to stop Brussels imposing the measure.

Robert Laszlo, a political analyst at thinktank Political Capital, said the setback would weaken Orban in dealings with the EU, even if it was unlikely to dent him at home, where his party enjoys a strong lead in opinion polls.

"This vote today is a temporary fiasco (for Orban), similarly to the referendum which was invalid," he said. "In Brussels, he will not be able to sell this as a success, there his positions will weaken."

Following the vote, Jobbik reiterated its demand that the government scrap a residency bond scheme under which foreigners can buy the right to live in Hungary for at least five years on payment of up to 300,000 euros ($331,000).

"THOSE ARE THE TRAITORS"

Almost 10,000 Chinese have taken advantage of the scheme to move to Hungary, as well as affluent investors from Russia and the Middle East. But Jobbik contends that some of the new arrivals pose a security threat.

During the vote, Jobbik lawmakers held up a large banner saying: "Those are the traitors who let in terrorists in exchange for money."

Jobbik leader Gabor Vona told reporters: "The moment that Fidesz, the government, scraps the bond program, Jobbik is ready to back the amendment of the constitution within 24 hours."

Jobbik is Fidesz's strongest opposition. The latest poll by research center Tarki showed its support at 10 percent in October, down from 14 percent in July. Fidesz widened its support to 32 percent from 30, with the opposition Socialists on 9 percent support. The next election is due in 2018.

Orban, in power since 2010, has cast the migrant debate as a question of national sovereignty that boils down to "whether a foreign population can be imposed on the people of an EU member state against its will".

He has linked migrants to terrorism, telling Hungarians before last month's referendum: "The more migrants there are, the greater the risk of terror."

The bill that failed to pass on Tuesday was designed to stop future migrant quotas being imposed on Hungary. It is already fighting an EU relocation scheme established during the height of the crisis last year, which set quotas for each EU country to host a share of the migrants for two years. Along with Slovakia, Budapest has launched a court challenge against that plan.

(Reporting by Krisztina Than and Sandor Peto; Editing by Mark Trevelyan)