Added auto-reconnection features

Dan, can you take a look at this. I've added auto-reconnect functionality.