ISLAMABAD, Pakistan - Pakistani authorities have banned, imprisoned, and killed Islamist militants who attack the state.

At the same time, the nation's powerful security establishment has tolerated and often nurtured what it considers "good" insurgent groups who help Pakistan's strategic interests by targeting archrival India, its eastern neighbor, and sowing chaos in Afghanistan, which sits to its west.

Picking and choosing among heavily armed ideologues has always been a dangerous game, but the risks for Pakistan were never more clear than on Tuesday, when Pakistani Taliban militants killed 132 children and 16 staff members at an army-run school in Peshawar.

The Pakistani Taliban, a federation of dozens of groups seeking to overthrow the government, is squarely in Islamabad's "bad" category. It has, however, forged close ties with other insurgents, such as the Afghan Taliban and anti-Indian groups, which operate from the same rugged border region, observe similar fundamentalist philosophies, and sometimes share foot soldiers.

With Pakistanis demanding the government respond to the Peshawar killings, Prime Minister Nawaz Sharif declared that "there is no difference between good Taliban and bad Taliban" and vowed to redouble military efforts "to clean this region of terrorism." He also called for swift punishment of militants.

But the challenges were immediately evident as Pakistan's antiterrorism court on Thursday granted bail to Zaki-ur-Rehman Lakhvi, the suspected mastermind of the 2008 terrorist attacks in Mumbai, India. The court decision came over the objections of a prosecutor, signaling the deep division in the army and political establishment over whether to break ties with the militants, who for years have been a cornerstone of the country's security policy, or make more use of them.

"There's reason to be skeptical of Sharif's statements because he's not the man in charge," said Raza Rumi, a Pakistan expert and senior fellow at the U.S. Institute of Peace in Washington. It was the army chief of staff, Gen. Raheel Sharif, no relation to the prime minister, who rushed to Afghanistan after the Peshawar attack to call for the handing over of Pakistani Taliban leaders believed to be living on Afghan soil, Rumi said.

Pakistan's policy has long vexed the U.S., its nominal ally, which blamed the army's spy agency, the Inter-Services Intelligence, for backing groups such as the Haqqani network that have carried out attacks on coalition forces in Afghanistan.

When the Pakistani army launched an offensive in the militant-infested North Waziristan tribal area this year, the United States, having urged such action for years, resumed drone strikes there against the Haqqani network and other suspected militants, often earning sharp rebukes from Pakistani officials.

Pakistan's tribal areas have been a haven for Islamist fighters since the 1980s when hordes gathered there to launch a jihad, or holy war, against the Soviet occupation of Afghanistan. After the 2001 U.S. invasion of Afghanistan, Pakistan granted sanctuary to many of its neighbor's ousted Taliban rulers, viewing them as a hedge against the chaos that could result when American forces withdrew.

Launched in the mid-2000s in response to Pakistani army operations in the border areas, the Pakistani Taliban took inspiration from the Afghan Taliban, pledging allegiance to its leader, Mullah Mohammed Omar.

In recent years, as the Pakistani Taliban insurgency has grown and tens of thousands of civilians have been killed, the group has forged alliances with sectarian militias and recruited fighters from radical madrasas.

"The policy of allowing militant groups to operate on Pakistani soil has proved disastrous," Husain Haqqani, a former Pakistani ambassador to Washington, wrote Thursday in the Indian Express. "Jihadi militants do not accept the neat divisions between global, regional and local conflicts."

Many analysts say Pakistan's determination to counter the growing might of India - its nuclear rival, from which it was carved in a brutal 1947 partition - fuels its support of militant proxies.

Even as Pakistan has vowed to crack down on extremists, groups that target India continue to flourish.

Rahimullah Yusufzai, a journalist who closely follows militant groups, said the Pakistani government would be unlikely to crack down on militant groups that focus on India.