Whatever their strength, American troops will not determine success in Afghanistan. Nor will the newly formed Afghan National Army. As U.S. forces are gradually withdrawn over the next three years, it is Pakistan’s 600,000-strong army that will become the dominant military force in the region and will try to shape its future. But that military is undergoing a deep internal crisis of identity, its most serious since Pakistan’s founding in 1947. How it resolves this crisis will determine its future, the future of the Afghan war — and much else.

This week’s news that a Pakistani brigadier general has been arrested for his ties to a radical Islamist group, Hizb ut-Tahrir, is only the latest in series of events that have rocked that nation. In the past year, two senior Pakistani officials have been gunned down, one by his own security guard. Last month, well-armed militants attacked a key naval base in Karachi, an operation that required inside assistance. Also last month, a brave Pakistani journalist, Syed Saleem Shahzad, who detailed the growing extremist presence within the Pakistani military, was tortured and killed, almost certainly by Pakistan’s Inter-Services Intelligence directorate (which denied the allegation). And then there is the case of Osama bin Laden, who was for years comfortably ensconced in an army town.

Pakistan’s military has traditionally been seen as a secular and disciplined organization. But the evidence is now overwhelming that it has been infiltrated at all levels by violent Islamists, including Taliban and al-Qaeda sympathizers.

There is also strong evidence of a basic shift in the attitude of the Pakistani military. Last month, Pakistan’s ambassador to the United States, Husain Haqqani, was invited to speak at the country’s National Defense University. Addressing a large gathering of officers, Haqqani asked the audience, “What is the principal national security threat to Pakistan?” He offered three categories: “from within [Pakistan],” “India,” and, “the United States.” A plurality voted for the third option.

The vote is consistent with a WikiLeaks document, a 2008 cable from Anne Patterson, the U.S. ambassador to Pakistan, expressing shock at the rising levels of anti-Americanism in the next generation of leaders of Pakistan’s military elite. Last November, Dawn, a Pakistani newspaper, described a military briefing in which a senior military officer explained to a handful of top columnists that the Pakistani military viewed the United States as a hostile force trying to perpetuate a state of “controlled chaos” in Pakistan and determined to “denuclearize” the regime.

Islamist ideology is replacing strategy. For 60 years, Pakistan’s military has focused obsessively on its rivalry with India. Large elements within that military appear to be switching obsessions, and the United States is replacing India as the organizing principle around which Pakistan’s military understands its national security interests. If this happens, not only is the Afghan war lost but Pakistan itself is also lost. (It does not have that far to fall; it made its annual appearance this year on Foreign Policy magazine’s “Failed States” list, coming in 12th, above Yemen.)

Ambassador Haqqani explained to his audience, “If [the threat] really comes from the United States then we’ve already lost, ladies and gentlemen, because you can’t beat the United States in a military confrontation. . . . [L]et us be honest, we do not have the means to take on the one military power in the world that spends more on defense technology than the next 20 nations in the world. So that is where I think we sometimes end up having what I call an ‘emotional discussion.’ ” Haqqani was gently pointing out the incoherence of these attitudes, but they persist.

It’s more than emotional. It is an indication that radical Islamist ideas — with America as the great Satan — are now reflexive for many in Pakistan’s military.

After the bin Laden raid, Pakistan’s military dispatched Prime Minister Yousaf Raza Gillani to Beijing to try to cozy up to the Chinese. “We have asked our Chinese brothers to please build a naval base at Gwadar,” said Chaudhary Ahmed Mukhtar, Pakistan’s defense minister, and Gillani returned claiming triumphantly that he had secured the deal. The Chinese, however, were too savvy to play this anti-American game and humiliated the Pakistanis by publicly contradicting the story.

Pakistan is drifting into a strategic black hole. Does the country really think its best path forward is as an adversary of the United States, currying favor with militants and becoming a vassal of China? Are its role models North Korea and Burma? Or does it want to crush the jihadist movements that are destroying the country, join the global economy, reform its society and become a real democracy? These are the questions Pakistan has to ask itself. The United States, for its part, having disbursed $20 billion in aid to Pakistan in the past decade — most of it to the military — needs to ask some questions of its own.