updated to include changes made by Shaun such as reconnecting when connection is closed.