HOUSTON — U.S. Sen. Ted Cruz, R-Texas, won a narrow victory Tuesday over U.S. Rep. Beto O’Rourke in the most expensive Senate race in U.S. history.

With nearly all the vote counted early Wednesday, Cruz held a 3-percentage point lead over O'Rourke, and the Associated Press had called the race for Cruz.

Cruz’s hard-won victory was closer than Democrats had any right to dream when O’Rourke, a little-known three-term congressman from El Paso, first set his sights on defeating Cruz, who in his first term in office ran a strong race for the Republican presidential nomination that, but for Donald , he might have won.

O’Rourke rolled up big margins in Travis, Harris, Dallas and Bexar counties and in the reliably Democratic border counties. He also expanded the electoral map for Democrats, winning Williamson and Hays counties, giving a boost to four Democratic Texas House candidates who defeated Republicans in GOP-held districts on the fringes of Austin.

But much of the rest of the Texas map remained red despite assiduous efforts by O'Rourke, who traveled to all 254 Texas counties in hopes of at least reducing the margins in the vast deep red stretches of exurban and rural Texas.

“The message is loud and clear, Texas remains solid red,” said Rafael Cruz, the senator’s father and early political mentor.

“The grassroots is much more intelligent than all the money in the world," said the elder Cruz, who said the message to "Robert Francis O'Rourke" — the Democratic candidate's full name — George Soros — the philanthropist and big donor to liberal causes and Democratic candidates — and Senate Democratic leader Chuck Schumer of New York is that "you cannot buy Texas."

O'Rourke, who refused to accept PAC money, raised $70 million, more than any Senate candidate in history, with a majority of the itemized donations of $200 or more coming from Texans. Cruz raised $40 million.

Sen. Cruz took a more conciliatory approach than his father.

“God bless Texas,” said Cruz, who took the stage at 10 p.m. “This election was a battle of ideas. It was a contest for who we are and what we believe in … and the people of Texas decide this race."

But Cruz said of O'Rourke, "He poured his heart into this campaign. Millions of people were inspired by his campaign."

Cruz, 47, said he was glad O'Rourke, 46, didn't prevail, but his message to his opponent's supporters was, "I am your senator as well. My responsibility is to represent every Texan."

"Credit goes to O'Rourke for getting close, but Ted Cruz deserves credit for surviving as strong a challenge in a midterm year as Texas has seen in almost 30 years," said University of Houston political scientist Brandon Rottinghaus.

The Senate race was an epic contest in every sense, matching equal and opposite talents who each in his time had come out of nowhere to become their respective party’s sensations both within Texas and beyond.

"It was a battle of titans, but it wasn't about Cruz and O'Rourke. It was about ideas," Cruz campaign manager Jeff Roe told reporters.

"He was a phenom," Roe said of O'Rourke, "I happen to think my guy was better, and winners get to write history."

When Cruz supporters, who were gathered at Houston's Hilton Post Oak by the Galleria for their victory celebration, learned that the race had been called for their candidate, they launched into a chant of "Build that wall," referring to the border wall that Trump has promised, but not yet secured much funding for from Congress.

"Beto’s success, combined with that of other statewide Democrats such as (attorney general challenger) Justin Nelson, should serve as a wake-up call for Texas Republicans," Rice University political scientist Mark Jones said. “The salad days of the Obama era are over, and if they are going to retain control of Texas they are going to need to recruit better candidates as well as perhaps utilize less polarizing rhetoric and pursue less polarizing policies, that is a little more (Texas House Speaker) Joe Straus and a little less (Lt. Gov.) Dan Patrick."

In El Paso, huge throngs of O’Rourke supporters flowed into Southwest University Park, the minor league ballpark that was the venue for what O’Rourke called a “Thank You Party.”

At shortly after 10 p.m. local time, O'Rourke took the stage to do just that.

"Tonight's loss does nothing to diminish how I feel about Texas or this country," said O'Rourke, who said he had spoken with Cruz, congratulated him on his victory and said he pledged to work with him to reduce divisions in the nation.

O'Rourke talked about his love for El Paso and, employing one of the epithets that has become an occasional token of his authenticity on the campaign trail, he told his supporters, "I'm so (expletive) proud of you guys," as confetti flew.

He exited the stadium with his wife, Amy, to John Lennon's "Imagine," and cries of "thank you Beto," and "Beto 2020."