The Iranian's remarks were taken as referring to the Sunni extremists that Tehran has been warning against since the start of the Syrian crisis. Ankara too has now come around to seeing these jihadist groups as a threat. It recently outlawed one of the main such groups, Jabhat al-Nusra (or Al-Nusra Front), after having been accused for some time of supporting it against the Assad regime. Events unfolding in Iraq immediately after Rouhani’s visit concentrated opinions in Ankara , while providing vindication for Tehran, which had accused Turkey, along with Saudi Arabia and Qatar, of aiding Sunni extremists in Syria.

Endorsing Gul’s sentiments, Rouhani, the first Iranian president to visit Turkey in 18 years, tellingly declared that the two countries were also determined to combat extremism and terrorism with a view toward showing the face of true Islam to the world. “The two countries are determined to increase cooperation in this area and to do what they can to increase security and stability in the region,” said Rouhani.

There was some evidence of a potential shift in the remarks of the Turkish and Iranian presidents during their joint press conference in Ankara on June 9. Referring to “the pain and destruction being suffered in the region,” Turkish President Abdullah Gul expressed his confidence that the joint efforts of Turkey and Iran would make a major contribution in overcoming this situation. “This is why I see this visit as a turning point,” Gul added.

It is no secret that Turkey and Iran are at odds over Syria, not to mention other issues, and have been engaged in what amounts to a proxy war, supporting opposing factions in that country’s sectarian civil war. The picture may be changing, however, with Syrian President Bashar al-Assad consolidating his power in regions under his control, while jihadist groups establish themselves in parts of northern Syria along the Turkish border as well as Iraq’s Nineveh province.

Iranian President Hassan Rouhani’s high-profile visit to Turkey June 9-10 aimed essentially to boost economic ties, as was evident from his being accompanied by the ministers of economy, oil, transportation and telecommunications along with the central bank governor and some 100 businessmen. Events in Iraq — i.e., the advances made by the Islamic State of Iraq and al-Sham (ISIS) as well as the raid on Turkey’s Mosul consulate and the taking of 49 hostages, including the consul general, by ISIS — now have the potential to also push Ankara and Tehran closer politically than in recent years.

As far back as August 2012, Maj. Gen. Hassan Firouzabadi, commander of Iran’s armed forces, cautioned all three countries by name against “becoming the victims of promoting al-Qaeda terrorism,” adding, “If, they accept such a norm, they must realize that after Syria, Turkey and other [such] states will be next in line.” His remarks appear prophetic in light of what has transpired. Compared to Turkey, whose miscalculations and mistakes in Syria, Egypt and Iraq are glaring, Iran is emerging as the country with focused and consistent policies that are bearing results and appear set to make Tehran a principal regional player.

Meanwhile Tehran and Washington are trying to overcome their ingrained mutual suspicion to resolve their deep differences on various issues. They are, however, on the same page with regard to Sunni extremism. This will be significant in the coming days and weeks as international responses to advances by ISIS and its affiliates in Iraq and northern Syria take shape.

Ankara and Tehran still disagree, of course, on Syria and Egypt, but it is Turkey that appears to be on weak ground in these respects. Ankara is therefore likely to be the party that gradually modifies its policies to match the reality on the ground. The belated designation of Jabhat al-Nusra as a terrorist organization, after numerous warnings from Washington and Tehran, already hints at change in this respect.

It was also telling that President Gul congratulated Egypt’s new president, Abdel Fattah al-Sisi, after his recent electoral success and expressed his belief that “deep-rooted Turkish-Egyptian relations will continue for all time.” Gul’s message is in stark contrast to Prime Minister Recep Tayyip Erdogan’s contention that the Egyptian presidential elections, which the Muslim Brotherhood was not allowed to contest, were not democratic. This represents a further sign of how Ankara’s diplomacy vis-à-vis the Middle East is wavering in the face of events over which Turkey has no control.

It is unclear where Saudi Arabia and other fundamentalist Sunni regimes in the Gulf stand on the question of ISIS. Robert Fisk, an expert on the region for the British Independent newspaper, maintains that ISIS and similar groups are funded by “Saudi Wahhabis and by Kuwaiti oligarchs.” Given developments in Syria and Iraq, however, Turkey is left with little choice but to distance itself from these groups and stop aiding them, if indeed it has been as claimed.

Deputy Prime Minister Bulent Arinc, meeting with a group of journalists on June 13 to discuss developments in Mosul, denied yet again that Turkey had ever supported any jihadist group in Syria or anywhere else. “We are definitely not, in any way, in any relationship with these groups. We are complying with all the decisions taken by the UN Security Council with regard to terror groups and their financing,” Arinc said.

The Turkish government is nevertheless rowing against a strong tide of established opinion that believes Ankara did in fact get into bed with these groups in the hope that they would not only bring a speedy end to the Assad regime, but also check the aspirations of the Kurds in Syria’s northern region, along the border with Turkey. Writing from Tehran, Reinhard Baumgarten, a regional expert for the German television channel ARD, argued in a June 12 article that the seizure of the Turkish hostages in Mosul by ISIS, which in addition to holding the staff of the consulate is also detaining nine Turkish truck drivers, was a response to Turkey’s shift away from aiding such groups. He further asserted that it was also the result of Turkey's disastrous foreign policy over the past three years, noting that Erdogan’s efforts to forge an alliance with Egypt, Saudi Arabia and Qatar — which have been referred to as the “Sunni axis” — against the Assad regime had failed.

The focus for now, however, has shifted to Iran — whose own “Shiite axis” appears to be holding — especially in regard to what steps Tehran plans to take in view of Iraqi developments, which it views as a strategic threat. The Rouhani government has already issued some high-level statements in this regard.

“As the government of the Islamic Republic of Iran, we will not tolerate the [acts of] violence and terror and we fight violence and terrorism in the region and in the world,” Rouhani was quoted as saying by Iranian media on June 12 after the headway made by ISIS. He reportedly added that the Supreme National Security Council of Iran would convene immediately to review developments in Nineveh province.

In the meantime, Iranian Foreign Minister Mohammad Javad Zarif reportedly held a telephone conversation with UN Secretary General Ban Ki-moon on June 12, warning him of the growing threat from "Takfiri terrorists" as the jihadists took control of parts of Iraq. According to Iranian media, Zarif also discussed the issue by phone with his Turkish counterpart, Ahmet Davutoglu, and other regional colleagues, during which he underlined the need for unity among Muslims, a joint and coordinated campaign against terrorism and support for the Iraqi nation and government.

Diplomats in Ankara expect this overall situation to force changes in Turkey’s regional policies, including increased efforts to overcome the tense relations the Erdogan government continues to have with Baghdad. The bottom line is that Erdogan and Davutoglu’s dream of a Turkey that would be the principal game-setter in the Middle East has been crushed. Instead, there is in its place a defensive Turkey that has painted itself into a corner and is being led by events that leave it no choice but to cooperate with countries that have much more regional influence that it does. Iran is clearly one of these countries.