Fix error on connect when file descriptor is invalid

This may happen if the bot looses the connection to the server and tries
to reconnect.