JERUSALEM -- Israeli Prime Minister Benjamin Netanyahu faced a deadline at midnight Wednesday to form a new governing coalition as he tried to stave off a crisis that could trigger an unprecedented second election this year or even force the longtime leader to step down.

That Netanyahu has struggled to secure a majority coalition in the 120-seat parliament was a shocking turn of events for the country's dominating political figure. He had appeared to secure a fourth consecutive term after elections on April 9, thanks to a strong showing by his Likud party and his other nationalist and religious allies.

The immediate cause of the standoff has been a dispute with Avigdor Lieberman, an ally-turned-rival who leads the small Yisrael Beitenu faction. Without Lieberman's five parliamentary seats, Netanyahu has no parliamentary majority.

But the deeper issue is connected to Netanyahu's legal troubles. Facing a likely indictment on corruption charges in the coming months, he wants his coalition partners to pass legislation that would grant him immunity and curb the powers of the country's Supreme Court.

Opposition parties strongly oppose granting Netanyahu immunity, robbing him of any alternatives to Lieberman as he forms his coalition.

"The fact that he is not a legitimate candidate for the premiership among the centrist parties is a huge contributor to this situation," said Yohanan Plesner, president of the Israel Democracy Institute think-tank and a former lawmaker.

A former aide to Netanyahu, Lieberman has alternated between being a close ally and a thorn in the side of his former boss for the past two decades. He has held a number of senior Cabinet posts, including defence minister and foreign minister.

Lieberman's base of support is fellow immigrants from the former Soviet Union, and he takes a hard line toward the Palestinians but also is staunchly secular.

He has demanded that the parliament pass pending legislation that requires young ultra-Orthodox men to be drafted into the military. Years of wide exemptions for religious men have generated resentment among the rest of Jewish Israelis, who are required to serve.

"I am not against the ultra-Orthodox community. I am for the state of Israel. I am for a Jewish state but against a Halachic state," Lieberman wrote on Facebook early Wednesday, using a term that refers to a Jewish state governed by Jewish law.

The ultra-Orthodox parties consider conscription a taboo, fearing that military service will lead to immersion in secularism, and insist the exemptions should stay in place. Netanyahu, dependent on the parties' political support, says they have compromised enough and refuses to press them further.

Netanyahu maintained contacts with Lieberman and other parties in hopes of forging a deal. But Lieberman also appeared to be sticking to his position.

Late Wednesday, Avi Gabbay, leader of the opposition Labor party, said he had rejected a last-minute offer to join the coalition that included a pledge by Netanyahu to drop his attempts to win immunity from prosecution. "Party members considered the offer and we decided not to accept the offer," Gabbay wrote on Twitter.

As the night wore on, parliament faced two other possibilities.

If no deal is reached by midnight, President Reuven Rivlin could choose another lawmaker, including opposition leader Benny Gantz or another member of Netanyahu's Likud, to try to form a coalition. Such a scenario, favoured by Gantz's Blue and White party, could bring a swift end to Netanyahu's 10-year reign as prime minister.

Instead, Netanyahu was pushing parliament to vote in favour of dissolving itself, a move that would trigger new elections in September. It would be the first time that Israel has held elections so close together. While the vote would mean months of paralysis and uncertainty, it would at least give Netanyahu the chance to regroup and try to win re-election.

Rivlin said if the deadline passed, he would consult leaders of different parties in hopes of finding a new candidate for prime minister, although the move to dissolve parliament was legal.

"I, for my part, will do everything in my power to prevent the state of Israel from going to another election campaign," he said.

During the day, parliament debated dissolution, with speaker after speaker addressing the hall. It was not known when a vote would take place.

In his speech, Lieberman showed no signs of compromise. "We've repeatedly said we want a draft law with the original composition. Nothing else," he said.

When Netanyahu's Likud party increased its power to 35 seats in parliament in last month's election and his traditional ultra-Orthodox and nationalist parties secured a solid 65-55 majority, it was assumed he would coast back into office comfortably. In no rush to sign agreements with the other factions, Netanyahu's allies in Likud had already begun drafting a contentious bill aimed at granting him immunity from the various corruption charges awaiting him.

Likud was also looking to push legislation limiting the power of Israel's Supreme Court and paving Netanyahu's path to many more years in office.

But no one seemed to take the mercurial Lieberman into consideration.

After trying to coerce a concession from Lieberman, Netanyahu and has allies have turned to an all-out offensive. They have accused Lieberman of betraying his voters and dragging the country to an unnecessary and expensive election campaign out of personal spite and jealousy of Netanyahu. Likud has also vowed to campaign hard among Russian speakers in Israel to try to wipe Lieberman out politically.

"Lieberman, climb down from the tree," Cabinet minister Miri Regev, a Netanyahu ally in Likud, wrote on Facebook. "If you drag the country once again to elections in half a year's time and you bring about the waste of over half a billion shekels ($150 million), there's no doubt that the people will have their say -- only Netanyahu!"

Whatever happens looks to complicate Netanyahu's precarious legal standing.

Israel's attorney general has recommended pressing criminal charges against him in three separate corruption cases, pending a hearing scheduled for October.

If elections take place in September and Netanyahu wins, it is unlikely he would be able to lock down the required political support for an immunity deal before an expected indictment. That would force him to stand trial.

"Early elections are very much not in the interest of the prime minister," said Plesner, the former lawmaker. "But he is more fearful of the mandate to form a government going to someone else."