Make close function pass close cause to close handlers, keep better track of the listening thread, close the listening thread when needed and set a timeout since the socket won't do it by it self.