IPOs are wildly profitable deals for Wall Street investment banks, and they come with huge bragging rights.

So it's no surprise that the biggest Wall Street banks fought over the Facebook IPO for years.

The inside story of the Facebook showdown reveals a lot about the relative status of Wall Street banks in Silicon Valley these days, especially Morgan Stanley and Goldman Sachs.

And it also reveals a lot about Facebook.

Facebook's in its IPO "quiet period" right now, and no one who knows anything is willing to talk publicly about it. This story is based on background interviews with more than a dozen Valley bankers, investors, and executives over the past few months, all of whom insisted on anonymity. (Some details from this story were also excerpted in this week's issue of New York Magazine, which you can read here.)

The story starts, as most Facebook stories do, with 27-year-old CEO Mark Zuckerberg, who used to tell colleagues he never wanted to go public...

* * *

In the Silicon Valley of the 1990s, not wanting to go public would have been heresy. Every tech startup in the Valley set its sights on an IPO, because going public meant getting pots of cash for the company, liquidity for shareholders, and a highly valued stock that you could use to scarf up other companies.

After the dotcom bust, however, the market for speculative IPOs evaporated, and many of the drawbacks of being public became clear:

You had to fight bad press and lawsuits whenever your stock dropped

You had to blow millions complying with SEC regulations

You had to persuade analysts to set estimates low so you could “beat expectations” each quarter even though you performed as expected

You had to explain your stock price—high or low—to your distracted employees, and,

You had to please impatient public-market investors, instead of focusing on creating great products and investing for the long haul.

In short, after the bust, many tech companies realized that being public was mostly a pain in the ass. And the private market soon created ways for companies to get the benefits of being public without going public—via large late-stage private investors or new marketplaces for private stock sales.

This was one reason Mark Zuckerberg didn’t want to take Facebook public: He didn’t need to. Facebook was able to get all the money and liquidity it wanted in the private market. The other reason was about control: Zuckerberg worried that going public might mean selling stock to shareholders who wanted Facebook’s first priority to be its business rather than its product—a fate he had spent Facebook’s entire history trying to avoid.

I'm staying private forever, baby. Getty Images / Justin Sullivan So Zuckerberg wasn’t kidding when he told early Facebook employees that he wanted to stay private forever.

And if a byzantine SEC rule hadn’t required Facebook to begin filing the same sort of financial disclosure as a private company as it would have as a public company, Zuckerberg probably would have postponed the deal even longer.

But that SEC rule—which forces companies to begin filing detailed documents when they have more than 500 shareholders—hit Facebook this spring. And if Facebook was going to go through all the hassles of being public, it might as well also get some of the benefits. Namely:

a highly liquid market for its stock, which would finally allow some of Facebook’s large institutional investors to sell (Facebook employees have been dumping stock via the private market for years), and

a public currency with which to make acquisitions (it’s difficult to use private stock to buy companies because the target company often thinks the stock is worth less than the buyer does).

Over the years, Zuckerberg had also become more comfortable with the idea that, thanks to the company's dual classes of stock, which gave him 57% voting control of Facebook with only a 28% financial stake, he would never have to give up control: Public shareholders could squawk about the way he ran the company, but they would have no legal way to force him to run it differently. Thus, as he saw it, he would never be forced to screw Facebook up.

So, last year, Zuckerberg finally became resigned to the idea of going public and pulled the trigger.

Which is to say, he delegated the bank-selection process to his Chief Financial Officer, David Ebersman, and went back to focusing on Facebook’s product...

For the past couple of decades, Goldman Sachs and Morgan Stanley have ruled the tech IPO business, with one of the firms serving as lead manager on most of the hottest deals.

Goldman took Microsoft, Yahoo, and eBay public, for example. Morgan won Netscape and Google. Although other firms have picked off an occasional deal over the years, when it comes to tech banking, Goldman and Morgan remain in a class by themselves. To be sure, having Morgan or Goldman take you public is no guarantee of success—they’ve banked plenty of dogs. But going public without Morgan or Goldman means signaling that you weren’t good enough for Morgan or Goldman. In other words, that your company is second-rate.

Microsoft went public in 1986. It was a Goldman deal. Ethan Hein via Flickr In the last few years, though, there has been a shift in the Morgan-Goldman power balance in the Valley. Specifically, until very recently, Morgan Stanley has won almost all of the hot IPOs.

Why?

Several factors, starting with the bankers involved.

Unlike other Wall Street businesses, corporate finance is still based on relationships: If you’re raising money or buying or selling your company, you’re hiring a person in addition to a firm.

And that person—the senior banker on your deal—is someone you’re going to have to interact with every day for months. Getting your deal done will be a frustrating process, because things rarely go according to plan. And the banker who wooed you during the courting period will always be tempted to delegate you to peons while he or she rushes off to woo someone else.

So you’d better like your banker, and trust his or her judgment. And he or she had better listen carefully to you, understand your business, and return your emails and calls.

Marc Andreessen and Netscape, the seminal Internet IPO of the 1990s. TIME In IPOs and other financings, banks can serve as either "lead managers" or "co-managers," with the former conferring vastly more cash and bragging rights. Within these categories, however, are other subtle but important distinctions. On Facebook, for example, three Wall Street banks were named “joint lead manager," but only one bank truly truly won the deal: The so-called "left lead." The other "joint leads," meanwhile, have impressive titles, but they are lackeys consigned “to the right.”

The “left” and “right” designations are literal: They refer to the bank’s relative positions on the cover of a company’s IPO prospectus.

The first bank listed, whose name is on the left side of the page, is the company’s primary banker. This bank manages the IPO process, runs the order book, and sets the IPO price. This bank also takes home the biggest bragging rights and largest share of the IPO payday. The names of the other lead banks on the IPO, meanwhile, appear to the right of the primary bank. Although these slots also confer huge bragging rights and fees, they are subordinate. In the eyes of other chest-pounding Wall Street bankers, being “to the right” means you lost:

Below the “lead managers” on the prospectus cover, meanwhile, are the co-managers, who get a smaller share of the IPO fees and bragging rights than the leads. On an IPO as big as Facebook’s, being a co-manager is still highly lucrative, but it’s miles from the prestige and money of being a lead.

Facebook's IPO is one of the biggest IPOs in history, so the glory and fees showered upon Facebook's lead banker were always going to be enormous.

If there has been one assignment that tech bankers and Morgan and Goldman wanted more than any other over the last few years, therefore, it has been to be selected by Facebook as the "left lead."

The IPO that started Morgan Stanley’s recent run of the tech table was LinkedIn, which went public in a blockbuster deal last spring. LinkedIn was all Morgan Stanley. Goldman wasn’t even a co-lead. And LinkedIn’s prestige and performance set Morgan Stanley up to win a whole string of big Internet deals that followed, including Pandora, Zynga, and Groupon.

The reason LinkedIn chose Morgan Stanley to lead its IPO, according to sources familiar with the decision, came down to the bankers involved and the way they wooed the company.

Morgan Stanley’s lead Internet banker is a 46-year old California native named Michael Grimes. Grimes has been at Morgan Stanley since the early 1990s, when legendary Valley banker Frank Quattrone ran the firm’s technology group. When Quattrone rocked the tech banking industry by defecting to a German bank in the late 1990s, Grimes stayed put. And he is now the co-head of Morgan Stanley’s global technology group.

Grimes is variously described as “a very strong banker,” “incredibly hard-working,” “honest, direct, and smart,” “weird,” and “a dork.” He is considered weird, one VC says, because he “plays video games until 5 in the morning.” Grimes is also “not a typical white-shoe banker.” Instead of getting an MBA at Wharton or Harvard Business School, Grimes went to Berkeley and graduated with degrees in Electrical Engineering and Computer Science. Such schooling would be a career liability at Morgan Stanley’s headquarters in New York, but in Silicon Valley it’s an asset. Most of the tech CEOs, board members, and investors that Grimes spends his life sucking up to are geeks, so it helps that he’s also one.

Grimes demoing a client's product. Facebook (The executive who called Grimes a “dork,” meanwhile, tells this story as evidence: Once, many moons ago, when Grimes was a mid-level banker, he received a call from a client in the middle of a conference room in which more than a dozen lawyers, bankers, accountants, and executives were sitting around a table drafting a financing document. Instead of simply excusing himself from the room to take the call, this executive says, Grimes slid under the table with his cell phone, There, he murmured audibly to his client while the drafting session continued.)

Most of Grimes’ biographical information is spelled out on his Wikipedia page, which is assiduously maintained. Some of Grimes’ competitors snicker about this, suspecting Grimes of hiring a PR firm to game Wikipedia and buff up his image. The competitors also hold this rumored PR firm responsible for a series of pro-Grimes puff pieces that appeared in the country’s most respected publications in the months before Facebook selected its underwriters—a press parade so obviously staged that one competitor described it as “hysterical.”

But whether or not Grimes professionally polishes his image, he has a good one to polish. His clients rave about him. And they hire Morgan Stanley to get him.

The same is not often said for a Goldman banker named Scott Stanford, who used to be Goldman’s main Internet banker in the Valley. Stanford competed with Grimes for the LinkedIn IPO and several other key deals -- which Goldman lost.

Scott Stanford looks good on paper--Harvard, Harvard Business School--and he has his supporters. He cultivated a relationship with the brilliant Russian investor Yuri Milner of DST, who charged into Silicon Valley a few years ago and rocked the establishment by shoveling hundreds of millions of dollars into some of the hottest companies (including Facebook). He also seems to play better in New York, where one Internet executive describes him as "perfectly smart," if not particularly impressive. But in the Valley, at least in some quarters, it's a different story.

In Valley tech-speak, the term “UI” is short for “user interface”—the part of a software program or gadget that you actually interact with. Scott Stanford's first challenge on LinkedIn, says one Valley executive familiar with LinkedIn’s IPO decision process, is that “his UI is off-putting.”

Pressed for details, the executive mentioned Stanford’s “slicked-back hair.” Morgan Stanley’s Grimes also has “UI issues,” this executive says—“he’s a banker—he talks very fast”—but Grimes is so substantive that you get past that. Stanford, meanwhile, “flew at a high level” and was “very theoretical” when describing LinkedIn’s business, which I think is a gracious way of saying that he was a blowhard. In contrast, Grimes and Morgan Stanley team were “very bright and very passionate about the business,” diving deep into the product arcana.

In sucking up to LinkedIn before the IPO, Stanford and Goldman also made three key mistakes.

First, they entrusted the LinkedIn relationship to a relative peon: Stanford was merely the head of Goldman’s Global Internet group, whereas Grimes was the head of Morgan’s whole technology group. There’s nothing that tells you about a firm’s commitment to your company than the rank of the banker assigned to you.

LinkedIn founder Reid Hoffman... a demi-god, but, in the case of the LinkedIn IPO, the wrong guy to stroke Second, Stanford sucked up to the wrong guy: He schmoozed LinkedIn’s founder and board member Reid Hoffman instead of the company’s CEO Jeff Weiner and CFO Steve Sordello, both of whom played a big role in the decision.

Third, Goldman got there too late.

For more than a year leading up to LinkedIn’s IPO, Michael Grimes and Morgan had been doing favors for LinkedIn’s executives—analyzing parts of their business, showing them deals, helping them with their finance operations—all the while building a relationship.

Goldman, meanwhile, showed up right before the IPO and said, basically, “Hi, we’re Goldman—hire us.”

Like most companies, LinkedIn held what is known as a “bake-off” before formally choosing its underwriters. Specifically, it invited bankers from several Wall Street firms to pitch its executives and board and make a case about why LinkedIn should choose their firms to manage the IPO.

Both Morgan and Goldman made strong pitches at the bake-off, a person who sat through them says. But Morgan included in its pitch-book a list of all the work Morgan had already done for LinkedIn, for free, while Scott Stanford was buttering up the wrong guy and Goldman’s bigger hitters were off focused on other deals. And the relationship Morgan built through all that free work all but sealed the deal.

Not that Goldman gave up easily.

In its LinkedIn pitch, Goldman rolled out what would become its new secret weapon in the Internet IPO wars, a former Army Ranger and 1990s-era Internet analyst turned banker named Anthony Noto. Noto was Goldman’s Internet analyst during the dotcom boom and bust. Like some of us, Noto made his share of lousy calls in the bubble, but unlike some of us, Noto survived them. Noto left Goldman in 2007 to become CFO of the NFL and then returned in 2010 as the co-head of Goldman’s Technology, Telecom, and Media (TMT) group, as the equal partner of a Valley-based banker named George Lee.

Anthony Noto, former West Point All-American football player, Army Ranger, Internet analyst, NFL CFO, and banker. When Noto first got back to Goldman, he was focused on media clients, and he watched helpless from New York as Goldman lost Internet deal after Internet deal. Then Noto began spending more time in the Valley. He rekindled his old relationships. He talked with venture capitalists and executives. And he discovered that part of Goldman’s problem was the people on the ground.

So Noto clipped Scott Stanford’s wings and brought in his own people.

(Stanford wasn't fired--he was just marginalized. Now, instead of calling on the hot companies, he gets to call on Yahoo. That's like a military general in the war theater being shipped off to Guam.)

Noto began taking over some of Goldman’s Internet relationships himself.

And he quickly improved Goldman’s reputation and business.

Unlike Michael Grimes and George Lee, Anthony Noto doesn’t have much experience as a banker, but he has experience as a research analyst and CFO, which many Valley executives find equally valuable. They like that Noto can see things from their perspective—the company perspective—instead of just the banker perspective. And they like that he still has relationships with big institutional investors and can talk intelligently about how investors think about stocks.

Noto is described as “relentless”—he refuses to take no for an answer. But his persistence is somehow humble and endearing instead of annoying. Noto works his ass off to try to get new clients, executives say, and then he works his ass off to make them happy when he gets them. More importantly, like Grimes, Noto is passionate about his clients’ technology products.

The best thing about Noto and Morgan Stanley’s Grimes, one Valley CEO says, is that neither are what you normally think of as “bankers.” They work hard. They carry their own bags. They’re eager to please. They display a humility that is often absent on Wall Street. “After all the crap about bailouts and Wall Street,” the CEO says, “the bankers have been so beaten down and humbled that they want to provide great service.”

To Valley executives, the return of Goldman Sachs to the tech IPO fray has been a welcome development: Now they can pit Morgan and Goldman against one another and make them both work harder to win deals. Or, like Facebook, they can hire both firms.

Anthony Noto arrived too late to save the LinkedIn deal for Goldman, but he has since helped Goldman turn its tech IPO business around. And in every deal since LinkedIn, Goldman has been regaining more ground. Goldman won a joint-lead role in the Zynga and Groupon IPOs last year, for example, and it won the recent Yelp IPO outright earlier this year. Most recently, Goldman has grabbed the lead spot for Glam Media and AutoTrader, both of which were big losses for Morgan Stanley and Grimes.

George Lee, co-head of Goldman's Global Tech, Telecom, and Media Group All that momentum, Goldman hoped, would put it in a good position to win Facebook’s IPO. After all, Goldman had not made the same mistakes with Facebook that it had made with LinkedIn: The Goldman banker responsible for the Facebook relationship was not Scott Stanford but George Lee, the co-head of the firm’s TMT group. And Lee had been rubbing Facebook’s shoulders for years.

Lee is a very experienced technology banker. He's also a man with his own UI issue, at least from the Valley's geek perspective: "He's cut from the traditional banker cloth," said one CEO, who added that to him all bankers are indistinguishable. And what is the traditional banker cloth? "He has a perma-tan."

(These UI issues, by the way, go both ways. The prototypical tech entrepreneur is socially awkward, with "bad hair" and grooming habits, and runs around the Valley in sandals. As a result, they make the money guys nervous.)

When you're massaging a company, you want to do everything you can to put your face in front of its executives, in and out of the office. And, when it came to Facebook, George Lee went step for step with Michael Grimes.

In the fall of 2010, for example, Facebook COO Sheryl Sandberg staged a gala benefit in San Francisco for the Cambodian women’s rights activist Somaly Mam. Events like this are a great way for bankers to demonstrate how much they care, and Morgan Stanley’s Michael Grimes wasn’t the only Facebook banker smart enough become a lead sponsor of the event.

The other lead sponsor was Goldman’s George Lee.

“Nobody cared more about Somaly Mam” than Grimes and Lee, one Valley executive joked later.

So Goldman was in an excellent position to win the Facebook IPO. And over the next 18 months it pulled out all the stops, including flying Goldman CEO Lloyd Blankfein out to the Valley a couple of times to schmooze.

Alas, when it came to Facebook, Goldman had another problem...

Andrew Ross Sorkin, whose scoop blew up a deal. On January 2, 2011, a startling article appeared in the New York Times. Written by the Times' star Wall Street reporter Andrew Ross Sorkin and a colleague, the article reported that Goldman Sachs had agreed to invest a staggering $450 million in Facebook at a price that valued Facebook at an equally staggering $50 billion.

Furthermore, Sorkin reported, Goldman was planning to offer its clients a chance to invest an additional $1.5 billion in Facebook at the same price via a private stock offering—in other words, Goldman was planning give clients access to a sweetheart pre-IPO deal of a super-hot company that everyone already assumed would ultimately go public at a spectacular price.

At first, this seemed a huge coup for Goldman. The firm was still struggling to overcome the horrendous publicity it had endured during the financial crisis. Rolling Stone writer Matt Taibbi had famously deemed Goldman a “vampire squid” and the name had gone viral. And Goldman had also recently had to pony up $550 million to settle a fraud claim brought by the SEC.

So the fact that Goldman had seduced the smoking-hot Facebook suddenly cast the firm in a lovely new light. By investing $450 million of its own money in the deal, moreover, the firm not only stood to make a monstrous pile of cash when Facebook eventually went public—it also appeared to have “bought” the IPO.

But then everything went to hell again.

Sorkin’s article blindsided Goldman, making public a deal that was supposed to conducted in secret.

The SEC’s rules around “advertising” private placements are strict: To protect investors from getting seduced by super-hyped stock offerings, the SEC prohibits brokerage firms like Goldman from contributing to any publicity around them.

Sorkin’s article hadn’t cited Goldman as a source for his story, but most observers assumed that Goldman had leaked it to him—for precisely the reason the SEC rule wanted to prevent: To whip up publicity and enthusiasm for the deal.

And, boy, did the article whip up publicity and enthusiasm for the deal.

After Sorkin’s article appeared, Goldman hastily sent out an email to clients announcing the offering—and was immediately deluged with orders for the Facebook stock. Over the next week, the press went into a frenzy. Then, alarmingly, the Wall Street Journal reported that the SEC had decided to look into the deal, to see if Goldman had violated its publicity rules.

After the SEC news broke, in a shocking move, Goldman suddenly announced that it was pulling the deal for its U.S. clients, for fear that the enormous publicity had violated the SEC’s rules. Instead, Goldman said, it would place the entire block of Facebook stock with its international clients.

Well, you can’t offer your best clients a sweetheart deal in a company like Facebook and then revoke the offer without pissing those clients off.

So Goldman’s U.S. clients were pissed off.

And so was Facebook.

The botched deal was a major embarrassment for Goldman. But more damaging to the firm’s chances of winning the Facebook IPO was the public animosity for Goldman that it stirred up again.

Facebook’s executives didn’t blame their Goldman bankers—led by George Lee—for the fiasco. But they were taken aback by the public hatred for Goldman.

The reaction at Facebook’s headquarters, one observer says, was basically this:

“Oh my god, we’ve gotten in bed with the vampire squid."

The blown private-placement, which should have cemented Goldman’s hold on the IPO, ended up crippling the firm’s chances of winning it. And because this deal played such a big role in the IPO decision, there’s another tantalizing story about it that’s worth sharing.

The story is that Goldman was not, in fact, the source of the leak to the New York Times—the leak that blew the cover off the private deal, triggered the SEC inquiry, and led to Goldman sustaining another huge blow to its reputation.

That story is that the leak came from JP Morgan or Morgan Stanley.

Before Facebook kicked off the Goldman investment and private-placement, this story goes, Facebook’s CFO David Ebersman placed courtesy calls to the senior bankers at Morgan Stanley and JP Morgan to let them know what was going on. This is standard practice: Ebersman had relationships at these firms that he would have wanted to maintain. The news that Facebook was doing a huge deal with a competitor, meanwhile, would not have been well-received by these bankers, who would have wanted it themselves. So, the calls from Ebersman, presumably, would have left these bankers feeling jilted and pissed.

So then, the story continues, feeling spurned by Facebook and knowing the impact the deal news might have on Goldman if it got out, one of these JP Morgan or Morgan Stanley bankers called the New York Times’s Sorkin and tipped him off.

Who was the tipster?

Jimmy Lee Maybe it was Jimmy Lee at JP Morgan, the story goes, the legendary head of the investment bank under Jamie Dimon (who also has a close relationship with Facebook). Maybe it was Morgan Stanley's Grimes, who is said to not be a gracious loser. Maybe it was Jamie Dimon himself.

For obvious reasons, this competitor-leak story is popular within Goldman Sachs. The idea that Goldman would be so ham-fisted as to blow up its own deal is mortifying to the firm, whose bankers need to be paragons of confidentiality and discretion.

Supporting the theory is the fact that it wouldn't have made sense for Goldman’s bankers to intentionally leak news of the deal. Given the excitement around Facebook, Goldman would have needed no external publicity to get the deal done.

This "Morgan nukes Goldman with a planted story” story is compelling—a tale of bad-ass corporate sabotage in keeping with the knife-fight analogy that that one banker used to describe the competition for the Facebook deal.

But the word from within the New York Times is that it's not that simple.

Andrew Ross Sorkin pieced together his scoop from tidbits gathered from multiple sources, a Times staffer says. Goldman’s competitors may have been among those sources, but it doesn't sound as though they planted the whole story in an attempt to sabotage the Goldman deal. More likely, it seems, some lower-level banker at Goldman let his excitement get the better of him, and told too many people about the deal.

Regardless, Goldman’s competitors can’t have been unhappy about the way things turned out.

When Facebook hired David Ebersman as CFO in 2009, it was with an eye toward the inevitable IPO. Ebersman would deal with Facebook's finances and Wall Street, neither of which Mark Zuckerberg had any interest in. And Sheryl Sandberg would continue to handle Facebook's business.

A former Wall Street analyst, Ebersman had worked for 15 years at biotech giant Genentech. Competition for the Facebook CFO slot was fierce, but after meeting Ebersman, Mark Zuckerberg knew he had found who he was looking for.