Join connection thread on disconnect.

Without joining the thread, an exception gets thrown when the interpreter shutsdown.
Add flag to signal disconnection, as assignment to the needs_reconnect flag can race with on_error.
Merge pull request #21 from tomwilkie/master

Fix some problems calling disconnect before exiting application.