'Closes the UDP socket if enabled and cancels all pending deferreds.
:return: A deferred that fires once the UDP socket has closed.'

@inlineCallbacks
def close(self):

    assert isInIOThread()
    (yield self.wait_for_deferred_tasks())
    self.cancel_all_pending_tasks()
    done_closing_deferred = succeed(None)
    if self.enabled:
        done_closing_deferred = maybeDeferred(self.port.stopListening)
        self.port = None
    res = (yield done_closing_deferred)
    returnValue(res)
