Connection loss, esp to gsm internet , internet switching

Hi, I’m using arduino mega, gsm sim900a, and esp8266 modules, how can I switch internet connection from esp8266 to gsm and vise versa when the arduino detects no internet connection from either esp or gsm … please help