This article first appeared on Foreign Policy's South Asia Channel.

For decades Pakistan has found itself caught between Saudi Arabia and Iran. These implacable antagonists have sought to enlist Pakistan's foreign policies behind their competing ideologies, geostrategic designs, and economic interests in Afghanistan, the Gulf area, and beyond. The rivalry also plays out on Pakistani soil, with Saudi Arabia and Iran often using violent proxies to vie for influence. Both states have a demonstrated capacity to reward -- as well as punish -- Pakistan.

Yet, the two are not evenly matched in their competition for influence in Pakistan. Saudi Arabia draws on the respect it accrues from being the spiritual center of the Islamic faith. Every Pakistani government has allowed Saudi Arabia wide berth to blunt Shi‘i and Iranian influence in the country through mosques and madrasas that disseminate a Wahhabi brand of Sunni Islam. Over the years, Pakistan's deep set of ties with the Kingdom has also been built on the exchange of Saudi Arabia's generous financial assistance for Pakistan's ample human capital. In addition to military personnel and training, Pakistan provides professionals and skilled and unskilled workers on whose remittances its economy heavily depends. By contrast, few Pakistanis find employment in Iran, and not since the post-1973 boom years of the Shah's reign has Iran been a possible source of direct financial assistance.

Iran holds one decided advantage, however: it shares a 550-mile long border with Pakistan. With already contentious borders with India and Afghanistan, Pakistan's leaders are eager to avoid a third, and have largely succeeded in keeping ties with Iran as normal as possible. Shi‘i Iran has the potential to stir unrest in a Pakistan already rent with sectarian strife, and both countries have reason not to stoke the other's ethnic Baloch insurgencies. Iran's close proximity makes its natural gas attractive to an energy-deprived Pakistan that must also weigh the possibility that its western neighbor may one day join it as a nuclear weapons state.

At times, Pakistan's policies have not pleased the Saudis and Iranians, who have in turn disappointed Pakistan. Bilateral ties have been closer with some Pakistani regimes than others. Yet, overall, there has been more continuity than variability, and relations with both countries have never been allowed to seriously deteriorate. A major reason is that total fidelity has never been demanded. Each has allowed the other leeway in crafting policies that respect their perceived core national interests. Moreover, neither Riyadh nor Tehran has presented Pakistan with stark "either/or" choices in which an Islamabad government had to choose openly between them.

During the 1960s and 70s, while Pakistan and Iran were formally allied militarily and economically, Pakistani trainers and advisors were stationed in Saudi Arabia and were providing a personal guard for the Saudi royal family. Despite pressure from Riyadh, Pakistan resisted taking sides in the 1980s Iran-Iraq war.  Iran's deep distraction during the decade helped to avoid a serious rift with a Pakistan eagerly taking Saudi money to aid Sunni mujahideen in the anti-Communist jihad. When, in the 1990s Pakistan and Saudi Arabia backed the radically Sunni Afghan Taliban against an ethnic opposition funded by Tehran, Pakistani regimes carefully managed to avoid directly confronting Iran. Abdul Qadeer Khan, father of Pakistan's nuclear program, is believed to have promised, and may have passed nuclear know-how, to Iran with the awareness of the Pakistani ISI. Khan was, meanwhile, a welcome visitor to Saudi Arabia. Although increasingly in recent years Pakistan's minority Shi‘i community has been targeted by Saudi-patronized sectarian terrorist groups, Islamabad's official condemnation has apparently placated Iran enough to forestall the kind of assistance that radical Shi‘i groups received from Iran in the 1980s.        

However, Pakistan's ability to maintain some balance in its relations with Saudi Arabia and Iran is now being tested. The Saudi royal family seems confident that it has in Pakistan's Prime Minister, Nawaz Sharif, someone strongly disposed to promote its interests. If not for the Kingdom's intervention, Sharif might have languished in prison or worse following his overthrow in 1999 by the military. The comfortable lifestyle he enjoyed in Saudi Arabia during seven years of exile, together with the religiosity and close ties of the Sharif family to the Saudi royals, would seem to have left him receptive. Indeed, a steady stream of high-level exchange visits by government and military figures has taken place since the general election in May 2013.

The Kingdom has openly bolstered Pakistan's sagging economy, the latest move being a $1.5 billion not-to-be repaid loan to Pakistan's treasury. Saudi Arabia has already had returns on its largess: Islamabad effectively backed out of a Saudi and U.S.-disapproved Iran-Pakistan gas pipeline agreement signed by former President Asif Zardari (no favorite of Saudi Arabia). Training of Saudi military officers has expanded under the Sharif government and, despite official denials, a policy of nonintervention in the Syrian conflict has ended, with Pakistan reportedly training and supplying with small arms Saudi-supported Syrian rebels.

This apparent tilt toward the Saudi regime notwithstanding, Sharif seems reluctant to tip the scale too heavily. Anxious to calm Iran's apprehensions, in late April Sharif sent his advisor on foreign affairs, Sartaj Aziz, to Tehran to convey to President Hassan Rouhani Sharif's message of "brotherhood, harmony and cooperation," and Sharif has just concluded a state visit to Iran. Pakistan's naval forces have also recently joined Iran's in joint exercises. In February, both countries moved quickly to defuse a crisis when abductors, presumably from Pakistan, kidnapped Iranian border guards.

The limits of Pakistan's willingness to please Saudi Arabia would be sorely tested were the Saudis to ask that Pakistani troops be overtly deployed in the Kingdom to confront Iran. Similarly, Pakistan can be expected to draw the line were the Saudi regime, faced with a nuclear-armed Iran, to request that Pakistan's nuclear arsenal be put at its disposal-whether to assist in building a Saudi nuclear program, to furnish warheads for Saudi missiles, or to provide a nuclear security umbrella.

Pakistan's nuclear program has always been India-centric and never about creating and sharing an Islamic bomb. Should Pakistan choose to proliferate its nuclear weapons, it is well aware that it would face international opprobrium and could expect severe censure. But above all, Pakistan is unlikely to abandon a decades-old policy of balancing Saudi Arabia and Iran that would leave it -- in addition to its two already contentious neighbors -- with an unfriendly, potentially nuclear Iran.

This article first appeared on Foreign Policy's South Asia Channel.

The Middle East Institute (MEI) is an independent, non-partisan, non-for-profit, educational organization. It does not engage in advocacy and its scholars’ opinions are their own. MEI welcomes financial donations, but retains sole editorial control over its work and its publications reflect only the authors’ views. For a listing of MEI donors, please click here.