Implement reconnecting upon connection loss

TODO: test in various conditions