This March 28, 2017, file photo, provided by the New York State Sex Offender Registry shows Jeffrey Epstein. Epstein is set to return to court Wednesday, July 31, 2019, to face sex trafficking charges just days after he was found injured in his cell. Credit: New York State Sex Offender Registry via AP

For the better part of two decades starting in the late 1980s, Jeffrey Epstein and Donald Trump swam in the same social pool. They were neighbors in Florida. They jetted from LaGuardia to Palm Beach together. They partied at Trump’s Mar-a-Lago Club and dined at Epstein’s Manhattan mansion.

And then, in 2004, they were suddenly rivals, each angling to snag a choice Palm Beach property, an oceanfront manse called Maison de l’Amitie – the House of Friendship – that was being sold out of bankruptcy.

Before the auction, Epstein and Trump each tried to work the ref; the trustee in the case, Joseph Luzinski, recalls being lobbied by both camps.

“It was something like, Donald saying, ‘You don’t want to do a deal with him, he doesn’t have the money,’ while Epstein was saying: ‘Donald is all talk. He doesn’t have the money,’ ” Luzinski said. “They both really wanted it.”

Only one man would win.

In the wake of Epstein’s arrest last month on sex trafficking charges, many who socialized with him – including Trump – are eager to have it known that they never much liked the man, or weren’t really friends, or barely even knew him.

“I was not a fan of his, that I can tell you,” the president said in the Oval Office the day after New York authorities took Epstein into custody.

But friends and associates said the two wealthy New York-to-Palm Beach commuters had socialized for years, drawn together by a mix of money, women and power.

“They knew each other a long time,” said Sam Nunberg, a former Trump aide who said he pressed the candidate about his ties to Epstein in late 2014 as the real estate mogul considered a White House run. “Bottom line, Donald would hang out with Epstein because he was rich.”

Their falling out, Trump said, happened about 15 years ago – several years before Epstein’s conviction on a prostitution solicitation charge.

Trump has not said why their relationship ruptured. “The reason doesn’t make any difference, frankly,” the president said.

Fifteen years ago, the two men squared off over the Palm Beach mansion. Just a few months later, local police began investigating allegations that Epstein was sexually abusing minors. Trump has also said – without providing details – that he at some point banned Epstein from Mar-a-Lago.

The White House declined to comment. Epstein’s lawyer did not respond to requests for comment.

It had been a typical Trump relationship: heavily chronicled in the news media, with an uncertain core beneath the surface.

Photos and articles captured the men together over the years, the future president of the United States and the future convicted sex offender: Epstein and longtime girlfriend Ghislaine Maxwell, Trump and his then-girlfriend, Melania Knauss, double dating at a celebrity tennis tournament at Mar-a-Lago. Partying with Britain’s Prince Andrew. Hanging out with National Football League cheerleaders. Dancing, laughing, palling around at a party Trump threw to celebrate his “freedom” after he divorced his second wife, Marla Maples.

“Terrific guy,” Trump said of Epstein in 2002. “He’s a lot of fun to be with.”

Within two years, public sightings of the two had ended.

– – –

Trump and Epstein were more than just neighbors who happened to end up at the same parties. They were two outer-borough New York guys, both with a knack for building their images and making a buck. Both attracted a ton of attention, though Trump worked hard to win notice and Epstein sometimes sought to deflect it. Both won reputations as men who were seen around many beautiful women.

In 2016, Trump Organization attorney Alan Garten told Fox News that Trump had “no relationship” with Epstein: “They were not friends and they did not socialize together.” Garten declined to comment for this article.

But Epstein, asked in a 2010 deposition if he had ever socialized with Trump, responded: “Yes, sir.”

The Epstein-Trump relationship didn’t exist in isolation but as part of a larger Palm Beach social swirl. In the early years after Trump bought the private Mar-a-Lago estate in 1985, Epstein and Trump were spotted together at Palm Beach events, including a pre-pageant dinner at Mar-a-Lago in 1992, according to people in attendance.

“They were tight,” said one person who observed them together and requested anonymity to avoid retribution. “They were each other’s wingmen.”

Trump, recently divorced from his first wife, Ivana, was in an on-and-off relationship with the woman he would soon marry, Marla Maples.

During that period, the New York developer, casting himself as a carefree playboy billionaire, hosted and attended parties at Mar-a-Lago and elsewhere, sometimes featuring models, cheerleaders and beauty pageant contestants. Trump had a business connection to all three industries: For a time, he ran a modeling agency. He owned a team in the United States Football League, a short-lived competitor to the NFL. And he controlled the Miss Universe pageant.

Since the start of his career, Trump had made his love life a central part of his public image. The idea was to build his brand as an avatar of fabulousness and to extend that brand by attaching beautiful women to his name, he has said.

“I create stars,” he said on ABC’s “Primetime Live” in 1994, adding: “I’ve really gotten a lot of women great opportunity. Unfortunately, after they’re a star, the fun is over for me. It’s like a creating process. It’s almost like creating a building. It’s pretty sad.”

Trump’s parties at Mar-a-Lago often featured models from Miami who floated around the patio and pool, with many more women than men, friends have recounted.

“That’s true,” Trump said in an interview in 2015, stressing he was single at the time. “The point was to have fun. It was wild.”

“There’s 100 beautiful women and 10 guys,” Roger Stone, his longtime adviser, told The Post in 2016. ” ‘Look, how cool are we?’ . . . I mean, it was great.”

Epstein, who in 1990 bought his own place in Palm Beach, two miles north of Trump’s, never became a member of Mar-a-Lago but visited the club for social events, Garten has said. On some of those occasions, Epstein was accompanied by Maxwell.

“Donald liked Epstein,” said Steven Hoffenberg, a Trump acquaintance who was Epstein’s business partner at a New York private equity firm in the 1980s and ’90s, until Hoffenberg was convicted of running a massive Ponzi scheme. “But he was crazy about Maxwell, a very charming lady.”

Epstein made several appearances at Mar-a-Lago. He attended a party there with NFL cheerleaders in 1992, where he was videotaped by an NBC news crew gathering footage for a segment on Trump. The network recently released the footage, in which Trump greets Epstein warmly and whispers in the financier’s ear, leading Epstein to double over in laughter.

Photographs and videos show Epstein and Trump posing together at the mansion in 1992, 1997 and 2000. The two were also pictured together, with model Ingrid Seynhaeve, in 1997 at a Victoria’s Secret party in New York City.

Around that time, Trump flew at least once, in the late 1990s or 2000, on Epstein’s private plane from Florida to New York, according to Epstein’s brother, Mark, who described the flight in a 2009 deposition.

In an interview last week with The Post, Mark Epstein said Trump flew on the plane “numerous times,” but said he was only present for one flight.

“They were good friends,” Mark Epstein said. “I know [Trump] is trying to distance himself, but they were.” He added that Trump used to comp Epstein’s mother and aunt at one of Trump’s Atlantic City casino hotels. When a Post reporter sought further details, Mark Epstein hung up.

When Jeffrey Epstein’s little black book of phone numbers appeared in a court file a few years ago, it contained 14 numbers for Trump; his wife, Melania; and others in Trump’s inner circle.

Trump and Epstein’s appearances together often made the news: In February 2000, Epstein and Maxwell attended a celebrity tennis tournament at Mar-a-Lago. Epstein brought along Prince Andrew, who was photographed with Trump and his then-girlfriend Melania Knauss.

Trump also dined at Epstein’s Upper East Side Manhattan mansion in 2003, according to New York magazine. “The dialogues are so engaging,” Epstein told the magazine, “that serving even the most extraordinary food sometimes seems inappropriate.”

But according to Stone, Trump turned down numerous invitations to Epstein’s private island and his Palm Beach home. In a 2016 book, Stone quoted Trump as saying that “The one time I visited [Epstein’s] Palm Beach home, the swimming pool was full of beautiful young girls. ‘How nice,’ I thought, ‘he let the neighborhood kids use his pool.’ ”

It was another prime property on Palm Beach island that pitted the two men against each other – a six-acre oceanfront estate with a 180-degree view of the Atlantic.

– – –

In November 2004, Trump, who was starring in NBC’s “The Apprentice” at the time, declared himself intent on winning “the finest piece of land in Florida and probably the U.S.,” an estate that had been seized as part of the bankruptcy of nursing home magnate Abe Gosman.

Trump said he planned to create “the second greatest house in America, Mar-a-Lago being the first” and then resell it.

Epstein was also enraptured by the property, and he, in contrast to Trump, seemed interested in living at the place. Harley Riedel, an attorney for Gosman, said the previous owner had filled the mansion with pricey art and “really did have in his heart that it would be nice if someone moved in and lived there.”

At first, Epstein pressed to gain the upper hand in the competition for the estate, according to Luzinski, the bankruptcy trustee. Epstein agreed on a price and terms that were viewed as favorable for Gosman’s creditors if a higher bid didn’t emerge, he said.

As the competition heated up, Trump and Epstein began talking each other down to the trustee, Luzinski said.

On Nov. 15, 2004, the bidders, their representatives, and a small cavalry of lawyers representing the creditors and the Gosman family gathered in a courtroom at the U.S. Bankruptcy Court in West Palm Beach. Trump was connected by phone.

The auction began with an attempt by one of Epstein’s three attorneys to knock Trump out of the bidding. Attorney Andrew Kamensky argued that Trump was not qualified because he demanded that the property have title insurance or he would not close on the sale. “What I’m telling you is that Mr. Epstein will – he will close,” Kamensky said, according a transcript obtained by The Post.

Trump wasn’t in Palm Beach – his own attorney, Raymond Royce, was in the courtroom. But Trump was on the phone, and now he chimed in to defend himself.

Riedel’s first notice that Trump might personally take part in the proceedings came when his voice boomed from the speakerphone. “I was sort of shocked,” the lawyer said.

Judge Steven Friedman rejected Epstein’s objection. The bidding began with Epstein’s offer of $37.25 million, but he dropped out after his bid of $38.6 million was topped.

Trump “had made up his mind to get it no matter the price,” said Charles Tatelbaum, a lawyer for one of Gosman’s creditors, JPMorgan Chase Bank.

A third bidder jumped in late, prompting Trump to pipe up again. “This is Mr. Trump,” he said over the speakerphone. “It seemed to be very clear that they dropped out also.”

The judge allowed the other bidder, Mark Pulte, to proceed, but Trump outbid him, too, with an offer of $41.35 million.

“I will therefore determine by the bang of the gavel that Mr. Trump is the higher bidder,” Friedman said.

In an interview, Luzinski described the showdown as “two very large Palm Beach egos going at it.”

It is unclear whether Trump and Epstein were in contact after the house sale. That month, Trump left two messages for Epstein at his home in Palm Beach, according to records obtained by Vice News – the last known interaction between the two men.

Four years after he bought the Gosman mansion, Trump sold it to Russian businessman Dmitry Rybolovlev for $95 million, more than doubling his investment.

– – –

It is unclear when Trump learned of allegations that Epstein was preying on teenage girls. In a 2002 interview, he gave no indication of concern, telling New York magazine that Epstein “enjoys his social life.”

“It is even said that he likes beautiful women as much as I do, and many of them are on the younger side,” Trump said.

On Nov. 28, 2004 – less than two weeks after the mansion auction – Palm Beach police fielded a tip that young women were seen coming and going from Epstein’s home, then-Police Chief Michael Reiter said in a deposition. Reiter declined to comment.

Four months later, in March 2005, police received a complaint from a woman who alleged that her 15-year-old stepdaughter had been paid $300 by Epstein to massage the financier while partially undressed, according to the police report. The Palm Beach police investigation identified more than a dozen possible victims, the report shows.

In 2006, a Palm Beach grand jury returned an indictment against Epstein of a single count of soliciting a prostitute. Epstein pleaded not guilty. That July, news organizations first reported that Palm Beach police had investigated Epstein for unlawful sex with minors and wanted the FBI to take up the case.

After a lengthy FBI investigation, federal prosecutors agreed not to prosecute Epstein under federal law, allowing him instead to plead guilty in state court in 2008 to two felony counts, including soliciting a minor.

Epstein is now facing federal charges in New York of sexually abusing dozens of girls. He has pleaded not guilty.

In late 2007, the New York Post reported that Epstein had been barred from visiting Mar-a-Lago, which Epstein at the time denied.

Earlier this month, Garten, the Trump Organization lawyer, said that Trump “banned him from stepping foot on the property.”

Nunberg said that when he quizzed Trump about his relationship with Epstein, Trump told him, “He’s a real creep, I banned him.” Trump told Nunberg that Epstein had recruited a young woman who worked at Mar-a-Lago to give him massages. Nunberg said Trump told him he issued the edict against Epstein years before the police investigation became public.

Epstein has also been accused of preying on a girl he met at Mar-a-Lago.

One of his alleged victims, Virginia Giuffre, has alleged in court documents that when she was a 16-year-old towel girl in Mar-a-Lago’s locker room in 2000, Maxwell “recruited” her to come to Epstein’s Palm Beach place to make money by giving massages.

Giuffre said in a lawsuit against Maxwell that Epstein sexually abused her at his mansions in both Palm Beach and Manhattan. That case was later settled out of court. Epstein and Maxwell have both denied taking part in any sex trafficking.

Trump also appears to have been helpful to Epstein’s accusers.

Brad Edwards, an attorney for some of the alleged victims, said in an interview last year that when he was seeking information from Epstein’s acquaintances in 2009, Trump was “the only person who picked up the phone and said: ‘Let’s just talk. I’ll give you as much time as you want. I’ll tell you what you need to know.’ “

Edwards declined to say what Trump told him but said he was “very helpful in the information that he gave.”

When Nunberg looked into Trump’s ties with Epstein, he said that Trump’s longtime secretary, Rhona Graff, and others in the Trump Organization all agreed that Trump had made a clear break with Epstein.

“That’s all I needed to know,” Nunberg concluded. “He’d never let somebody else get leverage over him.”

– – –

The Washington Post’s Manuel Roig-Franzia and Alice Crites contributed to this report.