TEL AVIV, Israel - With the vast majority of votes counted in Israeli elections by Wednesday morning, Israel's Prime Minister Benjamin Netanyahu looked set to clinch a fifth term in office, outmaneuvering corruption charges and a strong challenger.
Although both Netanyahu and his main rival Benny Gantz gave victory speeches to raucous crowds at their campaign headquarters a night earlier, only one of them can win.
With around 95 percent of the vote counted, both Netanyahu's Likud party and Gantz's Blue and White were tipped to win 35 seats in Israel's 120-member Knesset, or parliament.
But Netanyahu was the one with a clear path to forming a coalition, with his natural allies in the right-wing doing better overall, predicted to win 65 seats. To form a government he needs to cobble together a 61-seat majority in the Knesset.
Netanyahu was also a hairbreadth ahead when it came to the total number of votes, with 13,375 ballots more. Gantz told his supporters not to lose hope on Wednesday.
"Yes the odds are not in our favor, but two things are missing," he said, pointing to "a possibility of electoral shifts."
"The second is without a doubt the recognition of the hope we delivered to the Israeli people and society," he said.
But analysts suggest he was clutching at straws. "Gantz gave victory speech last night but woke up this morning with hangover," said Reuven Hazan, a politics professor at Jerusalem's Hebrew University.
Speaking to his supporters in the early hours of the morning Netanayahu said he wanted to thank them "from the bottom of my heart."
"It's an unbelievable, tremendous victory," Netanyahu said, his wife, Sara, at his side.
Netanyahu had presented the election as a referendum on his leadership, and his beaming smile showed he believed he succeeded in winning a new mandate from his people, despite his legal challenges. The attorney general in February recommended that the prime minister be indicted in three corruption cases, including on bribery, corruption and breach-of-trust charges, pending a hearing.
If he remains in power, Netanyahu is in a much stronger position to fight the charges and draw out the legal process, analysts say. If he forms a new government and survives until July, he will make history, becoming the country's longest-serving prime minister, outstripping Israel's founding father David Ben-Gurion.
With so much at stake, he pulled out all the stops in a campaign that attempted to strike fear into the hearts of right-wing voters that a "leftist" government may prevail, one that could team up with Israeli Arab politicians.
He forged a deal to ensure right-wing votes were not lost by pressuring small right wing parties to join with the extreme right wing Jewish Power party, toxic for even those at the far right of Israeli politics. That appeared to have paved their way to the Knesset, with the new alliance winning five seats, according to the partial results.
To woo more right-wing votes to his own party, he made a last minute promise to legalize settlements in the West Bank.
The party of right-wing Education Minister Naftali Bennett looked like it may not reach the minimum 3.25 percent of the vote needed to enter the Knesset, though military votes had not been fully counted.
The partial results point to the possibility of a more extreme right-wing and religious government than ever before, with ultra-Orthodox parties coming in with around 16 seats. That is unless Netanyahu decided to reach out to Blue and White - or parts of it - to form a unity government.
Gantz has publicly ruled out sitting in a government with Netanyahu.
In coming days, Israeli President Reuven Rivlin will nominate the leader of the party with the majority of support to try to form a government. Rivlin will make this choice after consulting with parties that have won Knesset seats.
What is certain is that Gantz, a former military chief of staff who first entered politics late last year, has put up a formidable challenge, even if he looks likely to fall at the final hurdle.
When Netanyhu called the election, he thought he would have a "comfortable walk" ahead, said Hazan.
"We saw a new party and a tight horse race," he said, adding that given the legal allegations hanging over his head, Netanyahu might not be celebrating for long. "This is his fifth term but it might end up being his shortest one," he said.
Meir Rubin, executive director of the right-wing Kohelet Policy Forum called Gantz's performance an "incredible achievement."
"He started his move three months ago and it looks like he could have won as many seats as the Likud party that's existed for more than 40 years," he said.
Overall voter turnout stood at 67.9 percent, dipping from 72.33 percent in 2015, amid reports of low voter turnout among Israeli Arabs. Making up 20 percent of the population, Israeli Arab voters had been frustrated by a split in the main Arab factions, while Israel's controversial Nation State law, bolstered calls for a boycott. However, one Arab faction, Balad-Ram looked set to scrape into the Knesset.
Israel's left-wing, meanwhile, was decimated after the split in the Zionist Union, which had brought together the Labor Party and former foreign minister Tzipi Livni's Hatnuah. While the Zionist Union had previously held 24 seats, Labor running alone won just six, according to partial results. Left-wing Meretz won four.
Despite the legal challenges he faces and the controversies he has courted, Netanyahu has a die-hard base that will vote for him unquestioningly. Michaela Ben Lulu, a lifetime Likud supporter, called Netanyahu a magician and said she admired his diplomacy, especially his relationship with President Trump.