Against a backdrop of rapidly rising tensions between Russiainfo-icon and the West, Prime Minister Imran Khaninfo-icon's long-planned February 23-24 visit to Moscowinfo-icon, the first official state visit by a Pakistani head of government in two decades, was always going to be a risky gambit.

But no one fully predicted how controversial its timing would eventually prove to be.

On February 21, Russia recognised two breakaway regions in Ukraineinfo-icon, Donetsk and Luhansk, as independent entities and ordered Russian troops to "maintain peace" there. Classifying the move as "the beginning of an invasion", the United Statesinfo-icon announced a first tranche of sanctions against Russia, including steps to starve the country of financing. Despite these developments placing the entire worldinfo-icon on high alert, Prime Minister Khan chose not to cancel or postpone his Moscow visit. And when Russia's President Vladimir Putin formally announced the invasion of Ukraine just before daybreak on February 24, eliciting swift global condemnation and sending international markets into a tailspin, Prime Minister Khan and his delegation were just settling in their hotel rooms in Moscow.

It is little surprise that the Pakistani visit to Moscow, primarily intended to boost energy and economic cooperation between the two sides and give impetus to the multibillion-dollar Pakistaninfo-icon Stream Gas Pipeline, immediately came under fire for its precarious timing. But it is also true that any eleventh-hour manoeuvring on the Pakistani side would have been fraught with difficulties.

The bilateral relationship between Islamabad and Moscow is not without significant scar tissue, left over from Pakistan's alignment with the USinfo-icon during the Cold Warinfo-icon, the deployment of Soviet troops in Afghanistaninfo-icon in the 1980s, and Russia's close defence ties with Pakistan's nemesis Indiainfo-icon. And there is much reason - both political and economic - for Pakistan to want to capitalise on recent efforts to reset the relationship.

Today, the Pakistani leadership is looking to urgently stabilise the country's inflation-hit economyinfo-icon by increasing domestic productivity and attracting foreign investment. To achieve its economic goals, Pakistan is aware that it needs to maintain regional peace and avoid getting entangled in the expanding mesh of global power politics.

This desire to maintain peaceful relations in the region has reinforced the Pakistani government's expressed commitment to increasing regional connectivity and encouraged it to expedite work on the Chinainfo-icon-Pakistan Economic Corridor, a core component of which includes the upgrading and rebranding of the country's maritime trade routes through the ports of Karachi and Gwadar.

Both Pakistan and China have maintained that these projects and routes are and will remain regionally inclusive. Pakistan's first-ever National Security Policy, unveiled earlier this year, reaffirmed the government's aspiration for regional integration and even contained tentative provisions for better relations with its archrival India. The document also called for the "reimagining [the country's] partnership with Russia in energy, defence cooperation, and investment".

In this context, Prime Minister Khan's decision to go forward with his scheduled meeting with Putin to demonstrate his desire for stronger ties and further collaboration with Russia could be seen as an inherently rational move. Russia has already agreed to invest in the Pakistan Stream Gas Pipeline between Karachi and the city of Kasur in Punjab - a project worth more than $2bn that would greatly help service Pakistan's ever-increasing energy demands once completed.

On the political and security front, Islamabad has reason to seek closer ties with Russia now, especially after the US's catastrophically executed withdrawal from Afghanistan in August last year. In other words, while Prime Minister Khan had much to lose from backing out of his planned visit to Moscow due to Russia's invasion of Ukraine, he also had little to gain.

Furthermore, in line with their desire to focus on the economy and good regional relations, Pakistani officials have long expressed their country's desire to steer clear of coalition-building exercises reminiscent of Cold War bloc politics. They have repeatedly made it clear, for example, that Pakistan's relationship with neighbouring China, which has undergone significant deepening since 2016, should not be misconstrued by the West as a zero-sum choice amidst growing Sino-US competition. Prime Minister Khan's decision to go forward with his Moscow visit despite the escalation in Ukraine - and the coalition-building activities it paved the way to - likely followed the same thinking.

Indeed, in a statement issued just before Putin and Khan's one-to-one meeting on February 24, Pakistan's foreign ministry said Prime Minister Khan "regrets" the "latest situation" between Russia and Ukraine and had hoped "diplomacy could avert a military conflictinfo-icon". The statement expressed Pakistan's opposition to the invasion but refrained from outrightly condemning Moscow, demonstrating Islamabad's desire to maintain careful neutrality on the growing tensions between Russia and the West.

That Prime Minister Khan's long-awaited visit to Moscow was overshadowed by the invasion of Ukraine, however, has likely only increased Pakistan's wariness of the many geopolitical tradeoffs that strategic closeness with Moscow entails. Tensions with the West notwithstanding, Russia also currently happens to be the largest arms exporter to India. The Indian Air Force is in the process of deploying Russian-made S-400 surface-to-air missileinfo-icon defence system batteries on its western border with Pakistan as a means to ward off airborne threats, and Indian Air Force officers and personnel have been receiving Russian training on these new systems.

India's defence and security closeness to Russia datesinfo-icon back to the Soviet era, back when the Kremlin actively backed India in the 1971 war that cost Pakistan its eastern wing. Pakistan is thus wary of becoming too close or dependent on Russia but does not want to harm strengthening relations and push this global power further into the arms of India either.

While it values its growing ties with Russia for several intersecting reasons, Pakistan's government is also not ready to give up on its tremulous but important relationship with the West.

Despite a recent dampening of relations, epitomised most notably by the absence of any direct communication thus far between Prime Minister Imran Khan and President Joe Biden, Pakistan's leadership recognises the necessity of keeping the Pakistan-US relationship off the ventilator. After all, the US is Pakistan's largest export marketinfo-icon and a leading potential investor in regional connectivity projects such as the 1,300-megawatt CASA-1000 energy project that supplies electricity to Afghanistan and Pakistan. Moreover, the IMFinfo-icon recently approved a $1bn disbursement to Pakistan under a $6bn loan programme that is likely to be spent financing imports and servicing foreign debt to prevent the Pakistani rupee from depreciating further.

On the strategic front, much of Pakistan's military equipment continues to benefit from American logistical and technical servicing. Furthermore, despite its disastrous withdrawal from Afghanistan, Pakistan continues to view the US as a necessary if reluctant regional security collaborator, as it recently demonstrated by inviting the US state department's special representative on Afghanistan to a Pakistan-convened Organisation of Islamic Cooperation emergency meeting on Afghanistan.

It is, however, is also not lost on Pakistani leadership that US policies towards South Asiainfo-icon are shaped by strategic considerations that often run counter to Pakistan's own. The US strategic framework for the Indo-Pacific, for example, has elevated India into a bulwark position against China, weakening Pakistan's hand against its hostile neighbour and encouraging it to seek other avenues of support to protect the delicate strategic balance in the region.

All in all, despite receiving vocal criticism from some quarters in the West, by going forward with his visit to Russia amid the invasion of Ukraine, Prime Minister Khan seems to have taken a calculated risk in line with what Pakistan sees as its current strategic, economic and political interests.

It is still not clear whether President Putin will accept Prime Minister Khan's invitation to visit Pakistan later this year. While five US presidents have visited Pakistan since 1947, a Russian head of state has yet to make the trip - so if Putin does agree to make the journey to Islamabad, he would be the first Russian leader to do so, and will send an important message about the future of Russo-Pakinfo-icon relations.

With an invasion of Ukraine under way and the US having now exited Afghanistan, Russia will undoubtedly also be paying closer attention to its southern flank than ever before, and will likely want to keep a close eye on the Talibaninfo-icon regime in Kabul. To this end, Moscow may well see closer contact with Islamabad as a means to shoring up contact with Afghanistan's new leadership.

Pakistan's own relationship with the Taliban is, of course, not uncomplicated, but thus far it has attempted to work in tandem with China, Russia and the US to help build a regional consensus that can undergird stability in Afghanistan's neighbourhood. In the end, Islamabad appears willing and able to work with Moscow - despite some underlying geopolitical contradictions - to stabilize South Asia, promote regional integration, and avert a humanitarian crisis in Afghanistan, even as one, paradoxically, may be beginning in Eurasia.