Minutes after news broke of a coup against Turkish President Recep Tayyip Erdogan, government-held areas in Syria broke out in celebratory gunfire, heralding what they believed was the removal of the leader they blame for fueling their country's five-year civil war.

Erdogan survived the insurrection, and judging by the surprise reversal of rebel fortunes in Aleppo this week, so has his government's support for the Syrian opposition. But Turkey, post-coup, is realigning, and as tensions with the West soar, Erdogan has shown a desire to mend fences with Russia, a key ally of Syrian President Bashar Assad.

On Tuesday, following talks in St. Petersburg, Russia, with President Vladimir Putin, the Turkish leader agreed to hold a separate discussion on Syria, involving top military and intelligence officials.

The meeting — Erdogan's first trip abroad following the July 15 failed coup attempt — comes amid boiling tensions over the contested northern city of Aleppo near the Turkish border, with both nations supporting opposing sides.

Here is a look at the Turkish involvement in Syria and how Turkey's latest post-coup realignment may play out:

HISTORY OF RELATIONS

Erdogan was among the first world leaders to demand Assad step down, calling him a "murderous butcher" after his forces opened fire on demonstrators months after protests against him erupted in 2011. It was a dramatic shift in relations between the two leaders, who had developed close personal ties and overseen the dramatic rapprochement between their two countries after they nearly went to war in the late 1990s over Syria's hosting of the main Kurdish leader, Abdullah Ocalan.

Erdogan had personally pushed back against U.S. pressure to isolate Assad at the start of the Iraq war. As Washington accused Damascus of allowing foreign fighters into Iraq, Erdogan invited Assad to vacation in Turkey, maintaining that good relations were necessary with his neighbor, who was helping him against Kurdish rebels seeking independence.

Erdogan also personally mediated between Israel and Syria in 2008 to resume peace talks, at a time when Ankara was on good terms with Israel.

Erdogan, whose Justice and Development Party has roots in Turkey's Islamic movement, is a strong backer of the Muslim Brotherhood, a global Islamic group that played an important role in the uprisings in Egypt and Syria, starting in 2011.

As a man who prided himself on being the main democratic leader with Islamist credentials in the region, this and his early bet that Assad's days were numbered, may be the single most important reasons behind the irreparable rift between the two leaders.

SUPPORT FOR REBELS

While residents in Assad strongholds celebrated the coup attempt against Erdogan as it unfolded last month, thousands of Syrians in Istanbul demonstrated in support of the Turkish leader. That's because Erdogan's government has been one of the most hawkish supporters of the predominantly Sunni Muslim insurgency against Assad. The country hosts most of the Syrian opposition and has served as a staging area for the fighters, providing them with offices, training facilities, intelligence and other logistical support.

As the only NATO country bordering Syria, its air bases have been used for coalition airstrikes against the Islamic State group. Border controls were an issue between Washington and Ankara, as foreign fighters trickled into Syria from Turkey in droves during the early years of the conflict.

Last year, amid growing frustration with U.S. inaction on Syria and casting aside Washington's concerns about aiding extremists groups, Turkey and Saudi Arabia started a new and aggressive strategy to help rebel groups bring about the fall of Assad.

After years of being at odds, the agreement struck between the two regional powerhouses led to the setting up of a joint command center in the Syrian province of Idlib. That coalition, called the Army of Conquest, wrestled control of Idlib province from Assad's forces last year, ushering in a Russian aerial campaign that began in September to help Assad's flailing forces.

"The Army of Conquest was a Turkish-led project, said Faysal Itani, a researcher with the Washington-based Atlantic Council.

That alliance, led by al-Qaida's branch in Syria, managed to breach the government siege on Aleppo this week.

An Aleppo activist said thousands of ethnic Turkic fighters, who have become an important feature of the battleground in Syria funneling through Turkey, were the decisive factor in the battle. Borders were also kept open for the wounded to travel out of Syria, he said, speaking on condition of anonymity because he was not authorized to speak on the matter to the media.

WILL THE FAILED COUP AFFECT SYRIA?

It's too early to tell whether the political turmoil in Turkey following last month's failed coup will affect Turkish involvement in Syria's civil war. But the massive and surprisingly well coordinated counteroffensive by rebels in Aleppo this week appears to have been supported at least in part by Turkey.

For years Turkey has built up insurgent groups in Aleppo province, which it used as a tool against Assad and its Kurdish enemies.

Aleppo is the most important battleground for Turkey, because of its proximity and its historic ties. The breach of the siege in Aleppo was a tactical win for Turkey, Itani said.

Ahmed Ramadan, a member of the Turkey-based Syrian opposition, said it is unlikely the Turkish-Russian rapprochement will undermine Ankara's support for the rebels. He said it was time for Moscow to realize there is no military solution in Syria.

"Aleppo battle pushes the political process forward and not the other way around," he said.

Erdogan, so far, has shown no sign of throwing in the towel.

"We don't want Syria's disintegration, but the departure of Bashar Assad," the Turkish leader said in an interview with Russia's Tass news agency. He added, however, that "mutual action by Russia and Turkey" was necessary to solve the Syrian conflict.

MENDING TIES

Tensions between Turkey and Russia peaked in late 2015 after Turkey shot down a Russian warplane it said had entered Turkish airspace from neighboring war-torn Syria. In June, Erdogan sent a much-anticipated letter of apology to Putin.

Sinan Ulgen, a former Turkish diplomat and visiting scholar at Carnegie Europe, said the issue of Syria is the most difficult topic facing the two leaders as they met Tuesday, and mending ties is unlikely to cause dramatic changes in Ankara's policies toward Syria given its large investment and public support for the Syrian opposition over the years.

Still, Turkey has had to down scale its ambitions in Syria amid lack of enthusiasm from the international community for regime change, Ulgen said.

That may make Turkey more willing to push opposition groups it has influence over, on the political track, he said. "That is something Turkey and Russia could agree on," he said.

Syria and its allies have blamed Turkey and Saudi Arabia for the failure of previous rounds of talks to try to end Syria's civil war, by backing opposition demands for Assad's removal.

For that reason, the latest rebel gains in Aleppo battle are significant for Turkey as well as the opposition.

While the opposition demonstrated its military abilities, "Turkey demonstrates to the international community that it has something to bring to the table," Ulgen said.

AL-QAIDA ON THE RISE

The breach of the siege in Aleppo was a tactical win for Turkey and its allies, Saudi Arabia and Qatar, in which the rebranded al-Qaida played an important role. Known as Jabhat al-Nusra, or the Nusra Front, before breaking ties with al-Qaida, the rebel alliance was key to the battlefield victory.

"Turkish thinking about Jabhat al Nusra has always been that it could be folded into the broader insurgent movement and thus was a manageable potential threat," Itani said. "If that is what's happening, then good for Turkey. But the group has a longer-term goal in mind — to dominate the insurgency."

"It would not be in Turkey's interest at all to have al-Qaida controlling northern Syria," he said.