fixed reconnection problem

It seems to be working according to my manual tests.
I will write an automatic tests, as soon as I learn how to do it.