Terence Winter battled a nagging thought as he wrote the pilot for HBO’s series “Boardwalk Empire,” a sprawling period epic that re-creates Atlantic City of the 1920s.

“I kept thinking ‘This is pointless. How can we possibly afford a boardwalk, or an empire?’?” Winter recalls. “We can’t call it ‘Boardwalk Empire’ and not see a boardwalk.”

Winter, a longtime writer-producer on “The Sopranos,” worried that even with Martin Scorsese as a producer, the project was too ambitious for the smallscreen. Winter had found in his research that the boardwalk back then amounted to “Times Square on the ocean.” Such scope seemed impossible to reproduce authentically for a TV series.

But when he turned in his first draft more than two years ago, HBO execs began weighing options for how it could be done. Ultimately, they rolled the dice, building a 300-foot boardwalk in an empty lot in Brooklyn for what would become an 80-minute $18 million pilot.

The project began to take shape just as HBO execs were battling their own nagging thoughts: how to create the next tier of breakout hits following the 2007 turning point, when “The Sopranos” ended its run and the cabler hit a rocky period when shows failed to impress — “John From Cincinnati,” “Lucky Louie,” “Tell Me You Love Me” — and some in the biz were quick to cluck that HBO’s reign was over.

“The great thing for us is that the bar is always high,” says HBO Entertainment president Sue Naegle, who has been in the job a little more than two years. “?’Sopranos,’ ‘Six Feet Under’ and ‘Sex and the City’ was an incredible time for us. But we can’t move forward by comparing now to then. We just can’t look backwards in that way.”

“Boardwalk,” bowing Sept. 19, is accompanied by pedigree (Scorsese also directed the pilot), a hefty promo campaign (plans include a screening event at one of the casinos in Atlantic City) and positive early buzz (the cabler sent out the first six episodes to journos and critics in late July). And it arrives as the cabler is enjoying renewed energy.

Naegle says the strong reception for new series like “True Blood,” “Hung” and “Treme” has paved the way for “Boardwalk” to make what execs hope will be a triumphant entrance just as the broadcast nets unveil their new seasons.

“It’s the perfect time for us to bring out something of this size and scope,” Naegle says. “This is what you want from pay cable. It’s beautiful and cinematic.”

Shows like “The Sopranos” and “Sex and the City” helped turn HBO into something larger than TV (or so alleged its famous “It’s not TV” tagline) in the late 1990s and early 2000s. The buzz and awards bestowed on the shows made HBO the destination for top talent — and helped the pay cabler sustain subscriber fees.

The success also paid off in the aftermarket, as “Sopranos” and others fetched a pretty penny in the off-net realm (A&E paid a record $2.5 million an episode for the show), while HBO fare also did well in international markets and in DVD sales.

The lean years ignited a new fire under HBO execs, who outlined ways to recapture some of the momentum that had slowed as its signature series went off the air.

Execs saw in Winter’s vision for “Boardwalk Empire” a chance to deliver grandiose storytelling. It was the kind of signature show that could only be done right if HBO committed to a big investment.

“As good as the script was, there’s no question this was a big swing, both creatively in terms of the storytelling and from a production standpoint,” says HBO programming prexy Michael Lombardo. “(But) if we’re going to do a period piece in the 1920s, you have to do it right. Atlantic City has to feel like a character in this piece.”

“Boardwalk” revolves around the private, public and criminal life of Enoch “Nucky” Thompson, the iron-fisted godfather of Atlantic City in the Jazz Age. The show weaves together a large ensemble cast and a multitude of storylines as it details an era when a prosperous nation plunged into the modern age. Steve Buscemi plays Thompson, the city treasurer who, with the aid of his brother, the sheriff (Shea Whigham), has his hand in the till of every gambling joint, and directs the traffic in illegal hooch after Prohibition is enacted in January 1920.

The “Boardwalk” saga illustrates everything from the temperance movement to the immigrant influx of the early 20th century to the assertion of racial identity by African-Americans and the tensions caused by that awakening. The cast of characters also encompasses major mob figures, including Lucky Luciano and Al Capone (mob underlings in those days), and the notorious Arnold Rothstein.

Not many crime dramas turn on the desire to examine history for parallels to our times, or examining how our standards of race, class, morality and gender roles were shaped by everything from the advent of the household vacuum cleaner to women’s suffrage. But Winter underscores the importance of HBO’s willingness to support unorthodox storytelling as part of its pay cable orientation.

“It boils down to (HBO having) tremendous respect for its audience, and having the faith that there are going to be people out there who want to watch challenging programming,” Winter says.

The biggest leap was the decision to build the boardwalk set (see separate story). Producers and HBO considered shooting on an existing boardwalk, such as in Asbury Park, N.J., but that would have presented logistical troubles and, significantly, wouldn’t have been able to take advantage of Gotham’s production tax incentives.

Fortunately, HBO’s previous work on its other period miniseries helped pave the way. Winter began to be convinced his pilot was filmable when he got a tutorial in how CGI was employed on “The Pacific” and “John Adams.”

The series was inspired by the 2003 nonfiction tome “Boardwalk Empire: The Birth, High Times and Corruption of Atlantic City,” by Nelson Johnson.

Winter was wrapping his six-season tenure on “Sopranos” in 2007 when HBO approached him about working with Mark Wahlberg and Stephen Levinson, the producing partners behind HBO’s “Entourage.” Levinson’s Leverage production banner had licensed the “Boardwalk” book, and HBO programmers were interested in a show that would invoke the colorful history of Atlantic City. Wahlberg had reached out to Scorsese, after working with the helmer on “The Departed.” Through their mutual tenpercenters (at what was then Endeavor), Wahlberg knew the helmer was interested in television.

Winter, however, didn’t know that Scorsese was involved when he agreed to take a stab at pulling a series out of the book. “On my way out the door they said, ‘Oh by the way, Martin Scorsese’s attached as a producer,’?” Winter recalls.

The scribe took months to research and write the script. He took a few fictional liberties with his central character, so he changed the name from Nucky Johnson to Nucky Thompson. The notion of Buscemi came out of a spitballing session with Scorsese, who mentioned that he wanted to work with the thesp, whom Winter knew well from “Sopranos.” (“If we were going to cast accurately what the real Nucky looked like, we’d have cast Jim Gandolfini,” Winter observes.)

HBO’s faith in the project’s scope was reinforced when Scorsese said he’d direct the pilot himself.

“We assessed the challenges, but for us it felt like there was no question — we had to do this show,” says HBO co-prexy Richard Plepler. “When you have Terry Winter and Marty Scorsese, it’s easier to take that leap.”

With Scorsese aboard, things started to coalesce.

“Scorsese is an actor magnet,” Winter says. “Everybody wants to work with him. I had all these pictures on my wall and I thought, ‘I’d really better write some good stuff for these people.’?”

Pre-production and planning for the pilot were extensive, and Scorsese’s prep and shooting sked for “Shutter Island” forced a delay in the pilot shoot, which finally lensed over a month in June 2009.

Scorsese and Winter scrutinized every detail, even making sure that the width of the wooden planks on the boardwalk were exactly the same as those in the 1920s. They had the costumers create clothes that were ill-fitting and handmade-looking, as most togs were for workaday folks in 1920. Several times during filming, period clothing wound up disintegrating on an actor in the middle of a scene, Winter says.

The 70-minute pilot required a big startup investment, although New York City production tax incentives helped a lot. Adding to the costs are the gaggles of extras and the extensive prep work required to dress sets, thesps and the surroundings of the show’s many location sites. (It usually takes advance crews about two days to cover up telltale signs of contempo fire hydrants, light fixtures, air-conditioning units, etc.) On top of that, there’s extensive CGI work in every episode.

The net cost of the pilot, including New York tax credits, came to $18 million. With the costs of the set and other expenditures now amortized over 12 episodes, “Empire’s” per-seg pricetag comes out to around $5 million, which execs say isn’t much higher than for its other top-tier series.

Winter recruited a number of “Sopranos” colleagues to work on various episodes, including helmer Timothy Van Patten, who had the unenviable assignment of following Scorsese in helming the second episode — on a far tighter schedule and budget.

Scorsese has kept an active hand, giving notes on scripts, casting choices and editing. He and Winter have a standing phone appointment on Sundays, to discuss “Boardwalk” and another HBO series they’re working with Mick Jagger. (It aims to chronicle a fictional record producer’s career from the early 1960s through the late 1990s.)

When lensing on “Boardwalk” ended in June, the wrap party held on the boardwalk set for some 700 people was a surreal experience, says exec producer Levinson.

“I expect them to be able to pull off the impossible,” Levinson says. “You never self-censor yourself there because they are so artist-friendly and production-friendly.”

“Boardwalk” has not formally been picked up for a second season, but that go-ahead seems virtually a fait accompli. “Nobody has asked us to dismantle the boardwalk,” Winter notes.

With the show’s premiere around the corner and “True Blood” hitting new heights in its third season, HBO’s current exec regime is clearly moving the cabler out of the shadow of its past glories.

Within the company’s walls, there is a renewed sense that HBO is “operating at full-throttle,” says Plepler, an 18-year company vet. Hopes are high for the new Dustin Hoffman horse-racing drama series “Luck,” from David Milch and Michael Mann.

Even if “Boardwalk” makes a big splash — the kind that helps lure and retain subscribers — Lombardo knows from 24 years of experience at HBO that they can’t afford to dwell on their successes … or failures.

“We rode the train through its peaks and valleys, (and) one thing we learned is never start patting yourself on the back about where you are,” Lombardo says. “Honestly, we’re all focused on what’s the next thing. We learned some hard lessons about the importance of never stopping to look for the next great show.”

‘Empire’ backbone built with boards, bluescreen