+ ret = snew(struct Socket_tag);
+ ret->fn = &fn_table;
+ ret->error = kOTNoError;
+ ret->plug = plug;
+ bufchain_init(&ret->output_data);
+ ret->writable = 0; /* to start with */
+ ret->sending_oob = 0;
+ ret->frozen = 0;
+ ret->frozen_readable = 0;
+ ret->localhost_only = local_host_only;
+ ret->pending_error = 0;
+ ret->oobinline = 0;
+ ret->oobpending = FALSE;
+ ret->listener = 1;
+
+ /* Open Endpoint, configure it for TCP over anything, and load the
+ * tilisten module to serialize multiple simultaneous
+ * connections. */
+
+ ep = OTOpenEndpoint(OTCreateConfiguration("tilisten,tcp"), 0, NULL, &err);
+
+ ret->ep = ep;
+
+ if (err) {
+ ret->error = err;
+ return (Socket) ret;
+ }
+
+ ot_setoption(ep, INET_IP, IP_REUSEADDR, T_YES);
+
+ OTInitInetAddress(&addr, port, kOTAnyInetAddress);
+ /* XXX: pay attention to local_host_only */
+
+ tbind.addr.buf = (UInt8 *) &addr;
+ tbind.addr.len = sizeof(addr);
+ tbind.qlen = 10;
+
+ err = OTBind(ep, &tbind, NULL); /* XXX: check qlen we got */
+
+ if (err) {
+ ret->error = err;
+ return (Socket) ret;
+ }
+
+ /* Add this to the list of all sockets */
+ ret->next = ot.socklist;
+ ret->prev = &ot.socklist;
+ if (ret->next != NULL)
+ ret->next->prev = &ret->next;
+ ot.socklist = ret;
+
+ return (Socket) ret;