Samanid rule in Balkh lasted until 997, when their former subordinates, the Ghaznavids, captured it. In 1006, Balkh was captured by Karakhanids, but Ghaznavids recaptured it 1008. Finally, the Seljuks (Turks) conquered Balkh in 1059. In 1115, it was occupied and looted by irregular Oghuz Turks. Between 1141 and 1142, Balkh was captured by Atsiz, Shah of Khwarezm, after the Seljuks were defeated by the Kara-Khitan Khanate at the Battle of Qatwan. Ahmad Sanjar decisively defeated a Ghurid army, commanded by Ala al-Din Husayn and he took him prisoner for two years before releasing him as a vassal of the Seljuks. The next year, he marched against rebellious Oghuz Turks from Khuttal and Tukharistan. But he was defeated twice and was captured after a second battle in Merv. The Oghuzs looted Khorasan after their victory.