Bill Gardner, the longest serving secretary of state in the nation, was elected to his 22nd term today, besting challenger Colin Van Ostern by just four votes.

The result came at the end of tumultuous, often confusing, day in the State House. A first round of voting yielded no clear winner; of 416 votes that lawmakers cast, Gardner got one more than Van Ostern - 208 to 207 - but he fell one vote short of the necessary majority.

Listen to NHPR's Casey McDermott break down what happened with the Secretary of State vote with All Things Considered Host Peter Biello.

In a second round of voting, Gardner edged ahead, winning 209 votes to Van Ostern's 205

Gardner enters the chamber to address legislators, to a standing ovation. #NHpolitics pic.twitter.com/yrQk4ItwL8 — Casey McDermott (@caseymcdermott) December 5, 2018

After 42 years in office, this was Gardner’s most serious challenge to date. And it came from a fellow Democrat who's tapped into simmering dissatisfaction over Gardner’s stance on election reforms in recent years.

The race pit Gardner, 70, against Van Ostern, a former gubernatorial candidate who's recently pivoted his focus to election reform. And it largely came down to a debate over whether Gardner's record of preserving the state's political traditions outweighs critiques of his handling of other responsibilities of the office.

Van Ostern, 39, has worked in marketing, served as an executive councilor, and was the 2016 Democratic nominee for governor. He also has a background as a political operative and raised nearly a quarter of a million dollars, largely from Democratic donors, toward his campaign to unseat Gardner — leading to concerns that he would undermine the Secretary of State's role as an impartial monitor of state elections.

Van Ostern seemed to have an edge over Gardner after Democrats took control of the Legislature in this year’s midterm elections. In an early straw vote of the incoming Democratic House caucus last month, Van Ostern won by a nearly 8-1 margin.

Among the catalysts behind this year's push to oust Gardner was his participation in President Trump's "Presidential Advisory Commission on Election Integrity." After accepting a seat on the panel, Gardner — who has otherwise enjoyed bipartisan support and a legacy free from major public controversy — faced pushback from critics who saw the commission as an attempt to justify the president's unsubstantiated claims of widespread voter fraud.

Despite the public pressure, Gardner didn't just defend his role on the panel — he doubled down on his involvement, hosting its second meeting in New Hampshire.

"New Hampshire people aren't accustomed to walking away or stepping down from their civic duties," Gardner said in his opening remarks at that forum, "and I will not, either."

The race has been closely watched both in New Hampshire and nationally because of the potential effects on the state’s first in the nation primary.

Five former New Hampshire governors — Republicans John Sununu, Judd Gregg, Steve Merrill, Craig Benson and Democrat John Lynch -- helped lead the pro-Gardner campaign over the past week, arguing that putting Van Ostern in the job would place the independence of the office and the presidential primary at risk.

New Hampshire’s current governor, Republican Chris Sununu, told reporters Tuesday that he was confident Gardner would “win overwhelmingly.”