better disconnection handling, closing thread automatically