 }
 
 let portNumber = UInt16(CommandLine.arguments.last ?? "") ?? UInt16(4000)
+let portValue = in_port_t(portNumber)
 let zero = Int8(0)
 let socklen = UInt8(socklen_t(MemoryLayout<sockaddr_in>.size))
 var serveraddr = sockaddr_in()
 serveraddr.sin_family = sa_family_t(AF_INET)
-serveraddr.sin_port = in_port_t(htons(in_port_t(portNumber)))
+serveraddr.sin_port = in_port_t((portValue << 8) + (portValue >> 8))
 serveraddr.sin_addr = in_addr(s_addr: in_addr_t(0))
 serveraddr.sin_zero = (zero, zero, zero, zero, zero, zero, zero, zero)
 serveraddr.sin_len = socklen
 
-guard bind(sock, sockaddrTypeCast(&serveraddr), socklen_t(socklen)) > -1 else {
-  fatalError("Cannot bind to the socket")
+withUnsafePointer(to: &serveraddr) { sockaddrInPtr in
+  let sockaddrPtr = UnsafeRawPointer(sockaddrInPtr).assumingMemoryBound(to: sockaddr.self)
+  guard bind(sock, sockaddrPtr, socklen_t(socklen)) > -1 else {
+    fatalError("Cannot bind to the socket")
+  }
 }
 
 guard listen(sock, 5) > -1 else {


repeat {
  let client = accept(sock, nil, nil)
  let headers = [
    "HTTP/1.1 200 OK",
    "server: Simple HTTP Server",
  ].joined(separator: "\n")
  let htmlResponse = "<!DOCTYPE html><html><body><h1>Hello from Swift Web Server.</h1></body></html>\n"  
  respond(client, withHeaders: headers, andWithContent: htmlResponse)
} while sock > -1
import Darwin.C

let sock = socket(AF_INET, Int32(SOCK_STREAM), 0)
guard sock > -1 else {
