anchor.diff:  	(push port *allocated-tcp-ports*)
   	(return port)))))
   
  +(defun close-tcp-connection (connection)
  +  (with-tcp-connection-locked connection
  +    (ecase (tcp-connection-state connection)
  +      (:syn-sent
  +       (setf (tcp-connection-state connection) :closed))
  +      (:established
  +       (setf (tcp-connection-state connection) :fin-wait-1)
  +       (tcp4-send-packet connection
  +                         (tcp-connection-s-next connection)
  +                         (tcp-connection-r-next connection)
  +                         nil
  +                         :fin-p t))
  +      (:close-wait
  +       (setf (tcp-connection-state connection) :last-ack)
  +       (tcp4-send-packet connection
  +                         (tcp-connection-s-next connection)
  +                         (tcp-connection-r-next connection)
  +                         nil
  +                         :fin-p t)))))
  +
   (defun tcp-connect (ip port)
     (let* ((source-port (allocate-local-tcp-port))
   	 (seq (random #x100000000))
  -	 (connection (make-tcp-connection :state :syn-sent
  +	 (connection (make-tcp-connection :%state :syn-sent
   					  :local-port source-port
   					  :remote-port port
   					  :remote-ip ip
  
positive.code:            (data-length (- end (+ start header-length))))
        (case (tcp-connection-state connection)
          (:syn-sent
           (if (and (logtest flags +tcp4-flag-ack+)
                    (logtest flags +tcp4-flag-syn+)
                    (eql ack (tcp-connection-s-next connection)))
               (progn
                 (setf (tcp-connection-state connection) :established
                       (tcp-connection-r-next connection) (logand (1+ seq) #xFFFFFFFF))
                 (tcp4-send-packet connection ack (tcp-connection-r-next connection) nil))
               (progn
                 (setf (tcp-connection-state connection) :closed)
                 (detach-tcp-connection connection)
                 (tcp4-send-packet connection 0 0 nil :ack-p nil :rst-p t)
                 (format t "TCP: got ack ~S, wanted ~S. Flags ~B~%" ack (tcp-connection-s-next connection) flags))))
          (:syn-received
           (cond ((and (eql flags +tcp4-flag-ack+)
                       (eql seq (tcp-connection-r-next connection))
                       (eql ack (tcp-connection-s-next connection)))
                  (setf (tcp-connection-state connection) :established))
                 (t (setf (tcp-connection-state connection) :closed)
                    (detach-tcp-connection connection)
                    (tcp4-send-packet connection 0 0 nil :ack-p nil :rst-p t)
                    (format t "TCP: Aborting connect. Got ack ~S, wanted ~S. Got seq ~S, wanted ~S. Flags ~B~%"
                            ack (tcp-connection-s-next connection)
                            seq (tcp-connection-r-next connection)
                            flags))))
          (:established
           ;; Ignore out-of-order packets.
           (when (not (eql seq (tcp-connection-r-next connection)))
             (format t "TCP: Ignoring packet with sequence number ~D, wanted ~D.~%"
                     seq (tcp-connection-r-next connection)))
           (when (eql seq (tcp-connection-r-next connection))
             (unless (eql data-length 0)
               ;; Send data to the user layer
               (if (tcp-connection-rx-data connection)
                   (setf (cdr (last (tcp-connection-rx-data connection))) (list (list packet (+ start header-length) end)))
                   (setf (tcp-connection-rx-data connection) (list (list packet (+ start header-length) end))))
               (setf (tcp-connection-r-next connection)
                     (logand (+ (tcp-connection-r-next connection) data-length)
                             #xFFFFFFFF)))
             (cond
               ((logtest flags +tcp4-flag-fin+)
                ;; Always ack FIN packets.
                (setf (tcp-connection-state connection) :closing
                      (tcp-connection-r-next connection)
                      (logand (+ (tcp-connection-r-next connection) 1)
                              #xFFFFFFFF))
                (tcp4-send-packet connection
                                  (tcp-connection-s-next connection)
                                  (tcp-connection-r-next connection)
                                  nil
                                  :fin-p t))
               ((not (eql data-length 0))
                (tcp4-send-packet connection
                                  (tcp-connection-s-next connection)
                                  (tcp-connection-r-next connection)
                                  nil)))))
          (:closing
           (cond ((logtest flags +tcp4-flag-ack+))
                 ((logtest flags +tcp4-flag-fin+)
                  (tcp4-send-packet connection
                                    (tcp-connection-s-next connection)
                                    (tcp-connection-r-next connection)
                                    nil
                                    :ack-p t
                                    :fin-p t)))
           (detach-tcp-connection connection)
           (setf (tcp-connection-state connection) :closed))
          (:closed
           (detach-tcp-connection connection))
          (t (tcp4-send-packet connection 0 0 nil :ack-p nil :rst-p t)
             (format t "TCP: Unknown connection state ~S ~S ~S.~%" (tcp-connection-state connection) start packet)
             (detach-tcp-connection connection)
             (setf (tcp-connection-state connection) :closed))))
      (mezzano.supervisor:condition-notify (tcp-connection-cvar connection) t)))
  
negative.code:        (format t "Establishing TCP connection. l ~D  r ~D  from ~X.~%" local-port remote-port remote-ip)
         (let* ((seq (ub32ref/be packet (+ start +tcp4-header-sequence-number+)))
                (blah (random #x100000000))
                (connection (make-tcp-connection :%state :syn-received
                                                 :local-port local-port
                                                 :remote-port remote-port
                                                 :remote-ip remote-ip
  