Fixed the code that reconnects to server