update to upstream version.
Add reconnecting if the gateway goes down, and make loading a new identity wait till the last one is totally disconnected to avoid disconnect while connect errors.