Add auto-reconnection

The quality of this code is scary bad :(