anchor.diff:    ((connection :initarg :connection :reader tcp-stream-connection)
      (current-packet :initform nil :accessor tcp-stream-packet)))
   
  +(defclass tcp-stream (gray:fundamental-character-input-stream
  +                      gray:fundamental-character-output-stream
  +                      sys.int::external-format-mixin
  +                      tcp-octet-stream
  +                      gray:unread-char-mixin)
  +  ())
  +
   (defmethod mezzano.network:local-endpoint ((object tcp-octet-stream))
     (let ((conn (tcp-stream-connection object)))
       (values (tcp-connection-local-ip conn)
  
positive.code: (define-condition connection-timed-out (connection-error)
    ())
  
  (defun tcp-listen (local-host local-port &key backlog)
    (multiple-value-bind (host interface)
        (mezzano.network.ip:ipv4-route (mezzano.network:resolve-address local-host))
      (declare (ignore host))
      (mezzano.supervisor:with-mutex (*tcp-listener-lock*)
        (let* ((source-address (mezzano.network.ip:ipv4-interface-address interface))
               (local-port (cond ((= local-port +port-wildcard+)
                                  ;; find a suitable port number
                                  (loop :for local-port := (+ (random 32768) 32768)
                                        :unless (get-tcp-listener-without-lock source-address local-port)
                                        :do(return local-port)))
                                 ((get-tcp-listener-without-lock source-address local-port)
                                  (error "Server already listening on port ~D" local-port))
                                 (t
                                  local-port)))
               (listener (make-instance 'tcp-listener
                                        :pending-connections (make-hash-table :test 'equalp :size backlog)
                                        :connections (mezzano.sync:make-mailbox
                                                      :name "TCP Listener")
                                        :backlog backlog
                                        :local-port local-port
                                        :local-ip source-address)))
          (push listener *tcp-listeners*)
          listener))))
  
  (defun tcp-accept (listener &key (wait-p t) element-type external-format)
    (let ((connection (mezzano.sync:mailbox-receive
                       (tcp-listener-connections listener)
                       :wait-p wait-p)))
      (cond (connection
             (when (tcp-listener-backlog listener)
               (decf (tcp-listener-n-pending-connections listener)))
             (tcp4-accept-connection connection :element-type element-type :external-format external-format))
            (t
             nil))))
  
  (defparameter *tcp-connect-timeout* 10)
  
  (defun tcp-connect (ip port)
    (multiple-value-bind (host interface)
        (mezzano.network.ip:ipv4-route ip)
  
negative.code: ;; DEFPARAMETER, not DEFCONSTANT, due to cross-compiler constraints.
  (defparameter +ip-wildcard+ (mezzano.network.ip:make-ipv4-address "0.0.0.0"))
  (defconstant +port-wildcard+ 0)
  
  (defparameter *tcp-connect-timeout* 10)
  
  (defvar *tcp-connections* nil)
  (defvar *tcp-connection-lock* (mezzano.supervisor:make-mutex "TCP connection list"))
  