Change the reconnect logic to gracefully handle the server going away

Change the reconnect logic to gracefully handle the server going away and coming back.