Pakistan’s diplomacy has become a high-wire act.

Though Islamabad hasn’t yet ended the domestic terror that rips through its schools, mosques and markets, or sorted out its hostilities with neighboring India, it has embarked on a complicated foreign policy. Seeking to solve its many economic, political and security problems, Pakistan is trying, concurrently, to court four rich and powerful patrons: China, Saudi Arabia, Iran and the United States.

This is a tough task because these governments’ relations with one another have global politics tied in knots. Islamabad’s efforts to juggle their competing demands are quixotic enough. Trying to make them all work in Pakistan’s favor is ambitious — and could easily backfire.

Pakistan has sought foreign protection and financing since its independence almost 70 years ago. It joined Western alliances in the 1950s. Pakistan let the United States use it as a foothold in south, southwest and central Asia throughout the Cold War, the Afghan wars, Pakistan’s wars with India, Washington’s opening to China, the rise of terror politics and recent tilts toward Asia.

Both Washington and Islamabad gained, and both lost. Alliances are like that.

But Pakistan has long tied its domestic governance to its foreign-policy engagements. With its various military alliances, a security state flourished, which almost inevitably diminished its capacity to democratize. Islamabad today reflects its on-again, off-again dictatorships and intermittent elected governments: Though parliament liberalized the country’s constitution, it also sanctioned military courts to try civilian offenders accused of terrorism.

Pakistan’s recent foreign-policy forays thus offer mixed messages to its people and the world. For the first time in a long time, Pakistan is trying to align its diplomacy to its pressing domestic needs. It hopes to manage its external relationships — some toxic, some constructive — to buy time to sort out its own profound governance problems. It is a big gamble to try to balance its relationships with Saudi Arabia, Iran, China and the United States — and, of course, India and Afghanistan — in order to secure its economy and create desperately needed domestic stability.

Let’s start with Yemen. In April, Saudi Arabia asked Pakistan to contribute to its armed coalition to fight Houthi militia. Prime Minister Nawaz Sharif, no doubt out of habitual obeisance to a financially profitable (if ideologically complicated) relationship, initially agreed.

In a rare show of political independence, however, Pakistan’s parliament refused to sanction military participation. It argued that Yemen’s fight was not Pakistan’s, and that Saudi Arabia should no longer assume that Pakistan would do its bidding: Despite financial recompense, Saudi Arabia’s backing of the Taliban had proved terribly costly for Pakistan, in too many ways to count.

With an eye toward its own sectarian tensions — Pakistan’s substantial Shi’ite minority has been under vicious attack for years, as have other religious minorities — parliament explicitly refused to choose between Saudi Arabia and neighboring Iran in another regional proxy war. Islamabad’s offer to help mediate was politely ignored by all sides.

Islamabad’s unexpected demurral to Saudi Arabia’s request sets it on a different diplomatic course. It is cash-poor, investment hungry and desperate for energy. Though Saudi Arabia contributed $1.5 billion to Pakistan for foreign exchange and energy projects last year (a sum probably dwarfed by the Saudis’ underwriting to Pakistan’s treasury during the Afghan Taliban years), Pakistan is also betting on its future relationship with Iran, with which it shares a long and critical border with Afghanistan.

Pakistan desperately needs Iran’s natural gas and hopes a U.S.-Iran deal will pave the way for importing fuel. A prospective pipeline from Iran to Pakistan and China has been dubbed the “peace pipeline.” Islamabad also needs to secure its western border with Iran, and restive Afghanistan to balance its restive eastern border with India.

At the same time, Pakistan is shoring up its relationship with China — again, by trading energy for diplomatic support. Beijing recently completed the construction of a major port at Gwadar, on Pakistan’s Arabian Sea coast, and has signed a deal to manage it for 40 years.

Ten days after refusing engagement in Yemen, Pakistan hosted Chinese President Xi Jinping to establish a China-Pakistan Economic Corridor that will run from the new port to Kashgar in Xinjiang province. The infusion of almost $50 billion is billed as a boon to Pakistan. In return, Islamabad has promised military protection for the Chinese construction workers. Meanwhile, China’s government and companies will no doubt make considerable profits.

China’s Asia-wide ambitions are becoming ever clearer. Indian Prime Minister Narendra Modi visited Xi last week to secure an evolving security partnership. While China cannot assume that its own diplomacy will lessen tensions between Pakistan and India, it is clear that money — and energy — talks across all populous, poverty-ridden south Asia. Pakistan’s benefits are derivative and echo a kind of colonialism: The country is essentially leasing itself to a foreign benefactor because it is too poor and inadequately governed to develop its own resources.

This potential cash boon leaves Pakistan with crucial domestic and foreign policy choices. The Chinese economic corridor’s route is already disputed in Pakistan: Sharif’s preference is to skirt the restive provinces of Baluchistan and Khyber Pakhtunkhwa in favor of his home province of Punjab. This continues a pattern of unbalanced investment that has contributed to the country’s uneven development.

What looks like a financial investment in stability is perceived as a slap in the face to Baluch insurgents. They have spent decades fighting for a voice in national politics and, ironically, a share in energy resources that originate in their home province.

Islamabad’s response has been brutal. Baluch have been intimidated, jailed, disappeared and killed since the 1970s. Most observers believe that the price of doing business with China will be to stifle the Baluch, impose a faux peace and broadly limit civil liberties.

This cost is too high — and most likely unnecessary. Pakistan has yet to contend with two issues that have slowed its development since independence. First, its refusal to provide equal citizenship rights for all has led to persistently corrosive subnational and sectarian tensions. This has soured relations among its provinces, slowed economic decision-making and reinforced a self-defeating propensity to solve political problems with state-sanctioned violence.

No amount of money will fix these difficulties. But serious, public discussions to help correct the misdeeds of the past and, most important, to organize responsibility for the future can help to craft policies that address these deep political fissures.

Second, Pakistan’s economic choices — often convenient for the rich and always devastating for the poor — have left deep holes in its budgets, financing and investment. Its energy shortfalls result from years of waste, misuse, circular debt and bad investments that now leave parts of the country without electricity for 10 to 20 hours each day.

Building roads from Pakistan to China — with Chinese labor — will not fix the poverty and absence of rights at the heart of Pakistan’s economic crises. Only better politics — starting with respect for Pakistan’s largely unorganized labor force and protection of critical rights for the entire public — can begin to ensure that China’s cash can turn into an investment for all Pakistan.

The burden of reconciling these competing demands is now Islamabad’s. Its challenges are critical: not to let foreign engagements provide cover for domestic political decisions; not to mask the political fissures that have created resource crises, and not to mistake motion for progress.

Pakistan cannot afford to fall off its high wire. Diplomacy can’t fix its governance, but even some initial improvements will be a big step toward securing its diplomacy.