(Updated 7:18 p.m.) In the most competitive Republican convention in the 5th Congressional District in 22 years, state Sen. Andrew Roraback finally prevailed on the third ballot, but he may face as many as three challengers in the Aug. 14 primary.

Roraback, who has been in the General Assembly for 18 years, led on all three ballots and secured the nod with 161 of the 294 delegates on the third ballot, followed by businesswoman Lisa Wilson-Foley of Simsbury, who collected 114 delegates on the final ballot. She was followed by former gubernatorial cabinet officer Justin Bernier of Plainville. Following several vote switches, Bernier fell to 29 delegates on the last ballot.

Litchfield businessman Mark Greenberg, who, like Bernier, ran for the seat two years ago, was dropped from the voting on the second ballot under the rule in which the candidate with the lowest total is taken out of the race on each roll call after the first ballot.

The last time a Republican convention in the 5th District went this many ballots was in 1990 when Waterbury Alderman Gary Franks won the nomination and then captured the seat in the sprawling district, which has elected seven congressmen since 1972.

“Democracy is a tough process,” Roraback said during his acceptance speech.

If elected, he pledged to promote the 294 Republican delegates’ values, including “limited government, lower taxes, and personal responsibility.”

Roraback edged Wilson-Foley 89 to 88 votes on the first ballot with Bernier taking 60 delegates and Greenberg 56.

On the second ballot, Roraback received 94 votes to Wilson-Foley’s 80, with Bernier at 62 and Greenberg at 58.

Former state Rep. Bill Hamzy of Bristol said Friday’s results bode well for Roraback with respect to ballot position and fundraising. Hamzy said Roraback will be near the top of the ballot along with Linda McMahon, who did really well in the 5th two years ago and who also won her senate nomination again Friday.

“If you’re not the endorsed candidate it also makes it difficult to fundraise,” Hamzy said.

The party’s endorsement gives Roraback, who didn’t get into the race until last October, a substantial boost.

Roraback said the endorsement will help him in what will likely be an expensive race, since Greenberg and Wilson-Foley, who ran for the GOP nomination as lieutenant governor two years ago, have already spent a considerable amount of their own money to help finance their campaigns.

“It’s not the end of the world,” Roraback said regarding the Aug. 14 primary. “This is the beginning of the process.”

Wilson-Foley said her second-place finish is not going to keep her from working hard and continuing to work hard to get to know all the voters in the 5th District.

“I know their dogs names,“ Wilson-Foley said of the delegates. “Literally.”

While Wilson-Foley, Roraback, and Greenberg will primary, Bernier still isn’t decided.

Bernier said he’s going to talk to his family and his supporters over the weekend about whether he will go forward with a primary.

“We’ll look at all the facts and we’ll make a decision in the near future,” Bernier said.

This is the second time in a row Bernier will have an opportunity to primary for the seat.

In 2010, former state Sen. Sam Caligiuri received 40 percent of the Republican primary vote to Bernier’s 31 percent. Greenberg received 29 percent that year even though he had to petition his way onto the ballot.

This year, Greenberg received enough support to automatically primary.

Greenberg, a real estate developer with many holdings in Connecticut and New York, declined to say how much of his own money he would spend,.

“Whatever is necessary to win,” he said after the convention.

Greenberg, who was not nominated at the convention two years ago, said he was able to build delegate support this year “by getting to know the delegates better and developing a rapport with them that I didn’t have two years ago.,”

He said he will be able to transition into the primary more easily this time since he will not have to spend the next few weeks collecting petition signatures to get on the ballot.

Former state Sen. Sam Caligiuri, R-Waterbury, who was the Republican nominee two years ago following the primary with Bernier and Greenberg, said he is becoming more optimistic about the Republicans’ chances of winning the seat.

The GOP had held it for many years up until six years ago when Democrat Chris Murphy of Cheshire won the seat in a stunning upset over Republican Nancy Johnson.

Caligiuri said the Democrats will probably make the seat a huge priority and there is a larger turnout in presidential election years, which typically favors Democratic candidates in Connecticut.

However, he said recent polling shows that Mitt Romney, the apparent Republican presidential nominee, is within striking distance of President Obama in Connecticut.

“I think Andrew can win a lot of the unaffiliated voters, which make up 45 percent of the electorate,” Caligiuri said. “I think he also can cut into the Democrats’ edge in the five major cities in the district — Waterbury, New Britain, Danbury, Meriden and Torrington.

Caligirui, who endorsed Roraback earlier this year, said a primary drains resources from the eventual nominee.

“I came out of the primary two years ago with $80,000 cash on hand and Chris Murphy had more than $2 million,” he said.

“But the dynamic is different this time because there is no incumbent and the Democrats will have to go through a primary,” Caligiuri said.

State House Speaker Chris Donovan of Meriden captured the Democratic nomination this last Monday night at a convention in Waterbury. He will be challenged in the August primary by former state Rep. Elizabeth Esty of Cheshire and Kent public relations consultant Dan Roberti.

Roraback, who was nominated by New Milford Mayor Pat Murphy, has been noted for his perfect voting record since arriving in the legislature in 1995 and his opposition to items on the Bond Commission agenda that he had deemed unnecessary, including a playground in his district.

Danbury Mayor Mark Boughton, who defeated Wilson-Foley two years ago for the GOP lieutenant gubernatorial nomination, nominated her for the congressional seat. Bernier was nominated by Waterbury Republican Town Committee Chairman Jason Van Stone and Greenberg’s name was placed in nomination by Wolcott Republican Town Committee Chairman Greg Daniel.

Christine Stuart contributed to this report.