         for socket in self.sockets {
             socket.shutdwn()
         }
-        self.sockets.removeAll(keepCapacity: true)
+        dispatch_sync(queue) {
+            self.sockets.removeAll(keepCapacity: true)
+        }
         socket.release()
         self.state = .Stopped
     }

        self.state = .Starting
        self.socket = try Socket.tcpSocketForListen(port, forceIPv4: forceIPv4)
        dispatch_async(dispatch_get_global_queue(priority, 0)) { [weak self] in
            guard let sself = self else { return }
            guard sself.operating else { return }
            while let socket = try? sself.socket.acceptClientSocket() {
                dispatch_async(dispatch_get_global_queue(priority, 0), { [weak self] in
                    guard let sself = self else { return }
                    guard sself.operating else { return }
                    sself.sockets.insert(socket)
                    sself.handleConnection(socket)
                    sself.sockets.remove(socket)
                })
            }
            sself.stop()
        }
        self.state = .Running
    }

    
    private var socket = Socket(socketFileDescriptor: -1)
    private var sockets = Set<Socket>()
    private var stateValue: Int32 = HttpServerIOState.Stopped.rawValue
    public private(set) var state: HttpServerIOState {
        get {
            return HttpServerIOState(rawValue: stateValue)!
        }
        set(state) {
            OSAtomicCompareAndSwapInt(self.state.rawValue, state.rawValue, &stateValue)
        }
    }
    public var operating: Bool { get { return self.state == .Running } }
    private let queue = dispatch_queue_create("swifter.httpserverio.clientsockets", DISPATCH_QUEUE_SERIAL)
    
    public func port() throws -> Int {
        return Int(try socket.port())
