Added onDestroy function to cause the socket to be closed then, not when the object is destroyed.