Fixed threading bug when the connection is closed from the connection thread.