As this week's election results and exit polls continue to make grim reading for Benjamin Netanyahu, Israel's longest-serving prime minister not only faces the possible end of his political career but also a probable court appearance and potentially even jail due to allegations of corruption.

Netanyahu had hoped to pass legislation that would prevent him from being indicted on multiple allegations of corruption — but he can only do so if he remains prime minister. Netanyahu denies any wrongdoing and has said he is the victim of a politically orchestrated "witch-hunt" by the media and the left. But he may have to make that case in court.

With 97 percent of the votes counted as of Thursday afternoon, both Netanyahu’s right-wing bloc in the Knesset and the centrist and left-wing bloc led by the Blue and White party fell short of the 61 seats needed to form a government in the 120-seat Parliament.

In a video message Thursday, Netanyahu called on the Blue and White party’s leader, Benny Gantz, to join him in forming a national unity government. Gantz gave his own speech saying he is also ready to lead a national unity government — but he refuses to serve under his rival.

In February, Israel's attorney general, Avichai Mandelblit, said he intended to indict Netanyahu on three separate charges of bribery, fraud and breach of trust. Mandelblit is due to decide whether to formally charge Netanyahu by the end of the year after a pre-trial hearing on Oct. 2.

Download the NBC News app for breaking news and politics

“In these 2019 elections of April and September, escaping court and possibly jail was Netanyahu’s political raison d’etre," said Gilead Sher, a lawyer and senior research fellow at the Institute for National Security Studies.

But experts said that even if Netanyahu remains in power, a divided Knesset could block any attempt to give him immunity.

“I don’t think there is a majority of government to give him immunity, even if he’s prime minister,” said Yossi Mekelberg, a professor of international relations at Regent’s University. “If he’s not prime minister anymore, it’s almost a certainty that he will face court.”

Without a governing majority of his right-wing bloc, Netanyahu will have to rely on coalition partners who are not his natural allies.

Israeli Prime Minister Benjamin Netanyahu delivers a statement during a news conference in Jerusalem Sept. 18, 2019. Ronen Zvulun / Reuters

With the pre-trial hearing in less than two weeks, Netanyahu is also running out of time to pass legislation.

“Very shortly these proceedings will begin making it virtually impossible for him to form a coalition government that would enable legislation to proceed that would stop those legal proceedings,” said David Halperin, executive director of the Israel Policy Forum, an organization that supports a two-state solution.

One possibility may be that President Donald Trump could lend Netanyahu a helping hand, giving him some leverage in negotiations to demonstrate that it was the wrong time for the prime minister to leave office, Halperin suggested.

But Netanyahu on Wednesday canceled a visit to the United Nations General Assembly in New York next week where he was due to meet Trump on the sidelines. And the president appeared to throw cold water on the idea that he would go the extra mile for Netanyahu, telling reporters Wednesday that “our relationship is with Israel.”

The outgoing Trump envoy for Mideast peace talks Jason Greenblatt was headed to the region Thursday to discuss the peace plan, a Trump administration official and an Israeli official told NBC News. He was expected to meet with both Netanyahu and Gantz separately although it was unclear exactly when.

NBC News reported earlier this month and that Greenblatt plans to leave the administration. Most recently he has suggested that he could stay a bit longer and perhaps until when the peace plan is launched. The Trump administration has said it would wait until the results of the Israeli elections before determining the right time to unveil the plan.

One card Netanyahu could play, according to experts, is negotiating a plea bargain. This would see him leave politics in exchange for not being indicted. His wife, Sara, did something similar this year when she admitted criminal wrongdoing over the misuse of state funds to order catered meals in a deal that saw her serve no jail time.

But experts point out that Netanyahu has dismissed the idea and has always denied any wrongdoing.

In any case, some observers say it would be foolish to dismiss Netanyahu’s chances of getting immunity entirely.

“I think we can assume that he will be offering the moon and more to whomever might be willing to join a coalition with him and certainly if they might consider advancing immunity legislation,” said Halperin.