Refactor and improve the reconnecting code and make it configurable

There is still the apparent problem that after reconnecting, event listeners
hvae to bre reregistered (which doesn't happen currently).

So messages can be sent but not received...
Register connection addHandlers when reconnecting