Check connected state and improve disconnect