fix #798 improve disconnect and reconnect logic