         return true
     }
     
+    fileprivate func createSocket2() -> Bool {
+        
+        // Create new socket
+        let newSocket: Int32 = socket(AF_INET, SOCK_STREAM, 0)
+        var c_sock: Int32
+        guard newSocket > 0 else { return false }
+        
+        var socketAddress = sockaddr_in()
+
+        withUnsafePointer(to: &si_host)
+        { sockaddrInPtr in
+            let sockaddrPtr = UnsafeRawPointer(sockaddrInPtr).assumingMemoryBound(to: sockaddr.self)
+            
+            if (bind(newSocket, sockaddrPtr, UInt32(MemoryLayout<sockaddr_in>.size)) < 0)
+            {
+                print("cannot bind server socket\n")
+            }
+            
+            if (listen(newSocket, SOMAXCONN) < 0)
+            {
+                print("cannot listen on server socket\n")
+            }
+            
+            // Set up a dispatch source
+            let newResponseSource = DispatchSource.makeReadSource(fileDescriptor: newSocket, queue: DispatchQueue.main)
+            
+            // Set up cancel handler
+            newResponseSource.setCancelHandler {
+                print("Closing UDP socket")
+                let UDPSocket = Int32(newResponseSource.handle)
+                shutdown(UDPSocket, SHUT_RDWR)
+                close(UDPSocket)
+            }
+            
+            newResponseSource.setRegistrationHandler {
+                print("register shite")
+            }
+            
+            otherSource = DispatchSource.makeSignalSource(signal:newSocket)
+            otherSource?.setEventHandler(handler: {
+                print("signaled")
+            })
+            
+            // Set up event handler (gets called when data arrives at the UDP socket)
+            newResponseSource.setEventHandler { [unowned self] in
+                guard let source = self.responseSource else { return }
+                
+                var socketAddress = sockaddr_storage()
+                var socketAddressLength = socklen_t(MemoryLayout<sockaddr_storage>.size)
+                let response = [UInt8](repeating: 0, count: 4096)
+                let UDPSocket = Int32(source.handle)
+                
+                let bytesRead = withUnsafeMutablePointer(to: &socketAddress) {
+                    recvfrom(UDPSocket, UnsafeMutableRawPointer(mutating: response), response.count, 0, UnsafeMutableRawPointer($0).bindMemory(to: sockaddr.self, capacity: 1), &socketAddressLength)
+                }
+                
+                guard bytesRead >= 0 else {
+                    if let errorString = String(validatingUTF8: strerror(errno)) {
+                        print("recvfrom failed: \(errorString)")
+                    }
+                    self.closeConnection()
+                    return
+                }
+                
+                guard bytesRead > 0 else {
+                    print("recvfrom returned EOF")
+                    self.closeConnection()
+                    return
+                }
+                
+                guard let endpoint = withUnsafePointer(to: &socketAddress, { self.getEndpointFromSocketAddress(socketAddressPointer: UnsafeRawPointer($0).bindMemory(to: sockaddr.self, capacity: 1)) })
+                    else {
+                        print("Failed to get the address and port from the socket address received from recvfrom")
+                        self.closeConnection()
+                        return
+                }
+                
+                print("UDP connection received \(bytesRead) bytes from \(endpoint.host):\(endpoint.port)")
+                
+                // Handle response
+                self.handler?(endpoint.host, endpoint.port, response)
+            }
+            
+            newResponseSource.resume()
+            responseSource = newResponseSource
+        }
+        
+        
+        
+        return true
+    }
+    
     /**
      Send broadcast message.
      

        
        self.handler = handler
        
        createSocket()
    }
    
    deinit {

    open func sendBroadcast(_ message: String) {
        
        if responseSource == nil {
            guard createSocket2() else {
                print("UDP ServerConnection initialization failed.")
                return
            }
