As a kid in the early ’80s, my Saturday afternoons were often spent taking the bus into Manhattan, then getting discount theater tickets with my parents. This was the pre-Giuliani Times Square, when it was less a well-scrubbed and obscenely priced tourist trap than America’s not-so-secret shame, and the walk from the bus terminal always took us through a gauntlet of porn theater marquees.

“Mom, what’s an ‘X’ movie?” I asked once, puzzled as always by the signs that my parents seemed afraid to even glance at.

My mother looked at me and said, with great solemnity, “Alan, it’s a place where lonely people go. And when they leave, they’re even lonelier than when they went in.”

The 42nd Street that inspired that bit of improvised maternal wisdom was the one born in the events depicted in The Deuce, the fantastic new HBO drama from The Wire gang — co-created by George Pelecanos and David Simon, its sprawling cast peppered with Wire alums like Lawrence Gilliard Jr., Chris Bauer, Gbenga Akinnagbe, and Method Man — in collaboration with higher-profile actor/producers like James Franco and Maggie Gyllenhaal. The title is the nickname for 42nd Street, in an era (the story begins in 1971) before pornographic films had gone mainstream, but when cops were on a first-name basis with every hooker and pimp on the block.

The Deuce (the premiere has been available On Demand for a few weeks, but the series officially debuts Sunday at 9; I’ve seen all eight episodes) recognizes the loneliness my mother tried to warn me about, but takes a more complex view of the business — including the way that some of its sex worker characters consider pornographic movies a big step up in safety and dignity from strolling the Deuce looking for clients. Those women are by and large on the street because they have no other choice — whether coming from extreme poverty and/or abusive homes — but some find a perverse form of freedom in the work, even as each detail of their lives is controlled by their pimps. The huge ensemble includes both characters who work directly in the sex trade and those who are adjacent to it spiritually and/or geographically, and the ones from the latter group often question the decisions made from the former. When a waitress wonders, for instance, why one of the prostitutes would return to the work even after getting a bus ticket out of town, an annoyed sex worker suggests, “Maybe she likes her life the way it is. You ever think of that?”

The series picks up shortly before a perfect storm of political and legal events — including NYC mayor John Lindsay’s long shot run for president, a series of court cases overturning various obscenity laws, and organized crime’s desire to increase their revenue from prostitution and pornography — slowly but inexorably pushes all the illicit activity from the sidewalks of the Deuce to massage parlors and theaters and adult entertainment emporiums indoors. (When one character sketches out a rough design of the first individual porn movie stall, it’s presented as a Eureka moment worthy of Archimedes himself.) It in many ways resembles the Hamsterdam season of The Wire — what if certain criminal activities in a major city were unofficially decriminalized? — only this one’s loosely based on real events.

Perhaps the most startling thing about The Deuce is just how much fun it is. This shouldn’t be a surprise, given how many laughs and thrills Simon, Pelecanos, and others were able to wring out of bleak Wire subjects like addiction, murder, and the overall collapse of the American Dream. Yet it’s still remarkable how tonally nimble the new show manages to be despite having no illusions about the psychic cost of selling one’s body day after day, year after year. There’s not just black comedy in this world, but more straightforward mirth and charm, like a scene where beat cop Chris Alston (Gilliard) takes Chinese delivery orders from all the women he just locked up on a periodic sweep of the Deuce.

Every character is given such a vivid inner life, and the show pulses with such energy, that it — like The Wire (and, to a lesser degree, Tremé) before it — manages to get away with various indulgences that have dragged down too many other recent dramas that have aspired to follow in Simon’s footsteps. The story drifts, with little delineation from one episode to another beyond “this is what happens next,” and it takes its sweet time getting around to the porno movie of it all, not really spending much time in that arena until the last few episodes. Doesn’t matter. The world and its citizens are so rich that it’s a pleasure to spend time in it at all, as written by this team, as directed by the great Michelle MacLaren and others (including Franco for a couple of episodes), and as performed by this superb cast.