On every continent, there is a pair of neighbouring countries that are very similar, yet different. And throughout history, their rivalry or cooperation has made all the difference between turmoil and stability, between war and peace. Take France and Germany in Europe, China and India, Japan and South Korea in Asia, or Argentina and Brazil in South America.
On the Eurasian landmass, one such pair is Iran and Turkey. Ever since the fall of the shah in 1979, Iran has held an unfavourable view of Turkey. Leaders of the Islamic revolution have always perceived the founder of the Turkish Republic, Kemal Ataturk, as an ally of Reza Shah, who founded the Pahlavi dynasty in the same period, and both were considered enemies of Islam. Also, Turkey, a member of NATO, was viewed as a close ally of the West, serving the interests of the United States and Israel. Furthermore, Turkey's effort to rise to the European model and join the EU was seen as a rejection of its Islamic heritage.
Today's Turkey is very different from the Turkey of the early 1980s and even from the Turkey of the early period of the ruling Justice and Development Party (AKP) in the mid-2000s.
For one thing, there is creeping Islamism eating at the fabric of secular Islam. Turkey today is deeply divided over its Muslim, secular and national identities and this schism is reflected in its foreign policy.
Relations with Israel have soured, with the US shaken, and the EU ambivalent at best. Neither Turkey nor the West value Turkey's NATO membership as they had before. Hence today, Iran views Turkey differently than it did in the early days of the Islamic revolution.
Last week, Iranian President Hassan Rouhani visited Turkey - the first president to have done so since 2008. Previous to this visit, Turkish Foreign Minister Ahmet Davutoglu had attended Rouhani's inauguration and Prime Minister Recep Tayyip Erdogan had been to Tehran. Clearly, there is a warming of relations between the two.
A possible nuclear deal between Iran and the West opens up entirely new cooperation opportunities for Iran and Turkey in the areas of security, economy and energy.
There are three factors that need to be considered in assessing and understanding the motives behind the Turkish-Iranian rapprochement and possible cooperation: the deepening divide between Shia and Sunni in the greater Middle East, the optimistic chatter about a possible nuclear deal between Iran and the West, and the alarming developments in Syria and Iraq.
And since Rouhani's visit to Ankara, a fourth factor has emerged which is more urgent than the other three: the recent and very consequential successes of the Islamic State of Iraq and the Levant (ISIL) in Iraq.
Throughout history, Ottoman Turkey and Persia were locked in a religious struggle for the soul of Islam, as well as ideological influence and power in the region. In the 1920s, the emergence of nationalism in both countries wiped away the intense ideological confrontation between the centres of Shia and Sunni global power.
After the Islamic revolution, Iran found no space for ideological confrontation with a secular Turkey. Since then, both sides have abandoned their extreme positions at the two poles, and have come to understand that the exploitation of Islam by fringe groups and terrorist organisations for quasi-political and sectarian aims could jeopardise the national and security interest of both countries. And it is up to the two states to begin to close the divide. Iran's recent overtures towards Saudi Arabia also had the same aim.
Turkey does not want to be faced with a nuclear Iran, fearing the emergence of an asymmetric power relationship with the Islamic Republic after centuries of balanced ties. Thus, a possible nuclear deal between Iran and the West opens up entirely new cooperation opportunities for Iran and Turkey in the areas of security, economy and energy.
Divided roles in Syria
Turkey and Iran haven't seen eye to eye on Syria either. But now, they have to. The issue is no longer one of Syrian President Bashar al-Assad's departure. Nor is it even about Syria. It is about the spread of al-Qaeda and its affiliates, and their reach, influence and possible penetration of other borders. The conflict now is about preventing changes in the map of the region.
Ironically, one may argue that today's situation in Iraq is also partly the extension of Turkey and Iran's opposing positions on Syria. While Iran helped sustain the Assad regime, Turkey helped the rebels and enabled their penetration into Syria. Now the conflict has extended to Iraq and evolved in such a way that ISIL successes have come to haunt both countries.
The new emboldening in Iraq directly affects the interests of both Turkey and Iran. The swift incursion made by ISIL fighters in northwestern Iraq last week, and the threat that they now present to the capital Baghdad, made everyone shiver.
If ISIL can hold Mosul and consolidate its presence there, it will have taken a giant step towards the break-up of Iraq along sectarian lines and the creation of an Islamist emirate that straddles Iraq and Syria. It could also lead to other changes to the borders that Britain and France imposed on the Middle East a century ago.
Turkey is increasingly concerned about the growing reach of ISIL and has already clashed with militants on its border with Syria. Turkey is especially wary of the potential for attacks by ISIL - attacks that would exploit the long border that runs from the Mediterranean to Iran.
Iran has long supported the regime in Syria, as well as, indirectly, Iraq's Nouri al-Maliki-led government in its fight against Sunni fighters. The growing reach of ISIL, and its ever-closer presence to Iran, has raised considerable anxiety in Tehran who has also hinted at assisting the United States in case of ground engagement in Iraq.
Today more than ever, the understanding and cooperation between Turkey and Iran is critical for the resolution of the persisting conflicts and the long-term peace and stability in the larger Middle East.
And finally there are the unpredictable developments in the Kurdish issue that could pit Turkey and Iran against each other. Both Iran and Turkey are influenced by each other's policies towards the Kurds.
Kurdish tensions build
In more recent history, Turkey and Iran have successfully kept the Kurdish problem out of their disagreements and tensions. Neither side used the Kurds on the other side of the border to incite instability and conflict. More than ever, they would like to keep it that way but that requires deeper cooperation on other issues of broader implications.
If the ISIL successes continue and the Baghdad government falls, there is no doubt that the Kurdish leadership will use this opportunity to achieve their long-sought independence. This clearly will change the entire nature and landscape, which will be at this stage, neither in Iran's nor Turkey's interest.
If there ever was a time in the last three decades, where there was a confluence of interests between them, it's now. Indeed, today more than ever, the understanding and cooperation between Turkey and Iran is critical for the resolution of the persisting conflicts and the long-term peace and stability in the larger Middle East. And it seems they both know it.
Vartan Oskanian is a member of Armenia's National Assembly, a former foreign minister and the founder of Yerevan's Civilitas Foundation.
The views expressed in this article are the author's own and do not necessarily reflect Al Jazeera's editorial policy.