Fix memory leak
Threading and fix of double freeing when connection breaks.
Switch on threading.