anchor.diff:                      logDebug (sprintf "[ReadOutgoing] %i available" messages.Length)
                       rep.Reply(Payload(messages))
                       return! loop [] None
  +            | CloseOutgoing ->
  +                logDebug "[CloseOutgoing] Closing"
  +                return ()
           }
   
           loop [] None
       )
   
  +    let onMailBoxError (x: Exception) =
  +        logError (x.ToString())
  +        this.Close()
  +
  +    do
  +        incomming.DefaultTimeout <- timeoutInMs * 2
  +        incomming.Error.Add(onMailBoxError)
  +        outgoing.DefaultTimeout <- timeoutInMs * 2
  +        outgoing.Error.Add(onMailBoxError)
  +        setPingTimeout ()
  +
       member val Id = id
  -    member __.ReadIncomming() = incomming.PostAndTryAsyncReply ReadIncomming
  -    member __.ReadOutgoing() = outgoing.PostAndTryAsyncReply ReadOutgoing
  +    member __.ReadIncomming() =
  +        lock closeLock (fun _ ->
  +            if closed then
  +                Async.result None
  +            else
  +                incomming.PostAndTryAsyncReply(ReadIncomming, timeoutInMs))
  +    member __.ReadOutgoing() =
  +        lock closeLock (fun _ ->
  +            if closed then
  +                Async.result None
  +            else
  +                outgoing.PostAndTryAsyncReply(ReadOutgoing, timeoutInMs))
       member __.AddOutgoing(msg) = outgoing.Post (NewOutgoing msg)
       member __.AddIncomming(msg) = incomming.Post (NewIncomming msg)
  -
  -type EngineIoSocket internal (int: InternalEngineIoSocket) =
  -    member val Id = int.Id
  -    member __.Read() = int.ReadIncomming()
  -    member __.Send(msg) = int.AddOutgoing(msg)
  +    member __.Close() =
  +        lock closeLock (fun _ ->
  +            if not closed then
  +                closed <- true
  +                onClose this
  +                pingTimeoutTimer.Stop()
  +                pingTimeoutTimer.Dispose()
  +                incomming.Post CloseIncomming
  +                outgoing.Post CloseOutgoing)
  +
  +type EngineIoSocket internal (socket: InternalEngineIoSocket) =
  +    member val Id = socket.Id
  +    member __.Read() = socket.ReadIncomming()
  +    member __.Send(msg) = socket.AddOutgoing(msg)
  +    member __.Close() = socket.Close()
   
   let inline private badAsync err = Async.result (Bad [err]) |> AR
   let inline private okAsync ok = Async.result (Ok(ok,[])) |> AR
  
positive.code: type IncomingCommunication =
      | NewIncomming of PacketMessage
      | ReadIncomming of AsyncReplyChannel<PacketMessage>
  
  and OutgoingCommunication =
      | NewOutgoing of PacketMessage
      | ReadOutgoing of AsyncReplyChannel<Payload>
  
  and Socket =
      {
          Id: SocketId
          Transport: Transport
          IncomingMessages: MailboxProcessor<IncomingCommunication>
          OutgoingMessages: MailboxProcessor<OutgoingCommunication>
      }
  
  [<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
  module Socket =
      /// Send a packet to the client. This is a non-blocking write.
      let send msg socket = socket.OutgoingMessages.Post(OutgoingCommunication.NewOutgoing(msg))
  
      let getOutgoing timeout socket =
          // TODO: If we timeout, kill the socket
          socket.OutgoingMessages.PostAndAsyncReply ((fun c -> ReadOutgoing(c)), timeout)
  
      let addIncoming message socket =
          socket.IncomingMessages.Post (IncomingCommunication.NewIncomming message)
  
  type EngineIoConfig =
      {
  
negative.code:     | UnknownSessionId
      | Unknown
  
  type InternalEngineIoSocket(id: SocketId, pingTimeout: TimeSpan, onClose: InternalEngineIoSocket -> unit) as this =
      let logError s = ll.error (eventX (sprintf "[%s] %s" (id.ToString()) s))
      let logDebug s = ll.debug (eventX (sprintf "[%s] %s" (id.ToString()) s))
      let logWarn s = ll.warn (eventX (sprintf "[%s] %s" (id.ToString()) s))
  
      let closeLock = new obj()
      let mutable closed = false
  
      let timeoutInMs = int pingTimeout.TotalMilliseconds
  
      let pingTimeoutTimer =
          let timer = new System.Timers.Timer()
          timer.AutoReset <- false
          timer.Interval <- pingTimeout.TotalMilliseconds
          timer.Elapsed.Add(fun _ ->
              logDebug "Ping timeout, closing socket"
              this.Close())
          timer
  
      let setPingTimeout () =
          pingTimeoutTimer.Stop()
          pingTimeoutTimer.Start()
  
      let incomming = MailboxProcessor<IncomingCommunication>.Start(fun inbox ->
          let rec loop (messages: Queue<PacketMessage>) (currentReplyChan: AsyncReplyChannel<PacketMessage> option) = async {
  