Shuffle gateway logic around for improved reconnecting.

Fixes #957
Changelog re #957