--- /dev/null
+#include<net.h>
+
+#define MAX_BACKLOG 10
+
+static int listen_sock();
+static int tcp();
+static int udp();
+
+#define HINTS_INIT(hints,socktype) { \
+ memset(&hints,0,sizeof(struct addrinfo)); \
+\
+ hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */ \
+ hints.ai_socktype = socktype; /* */ \
+ hints.ai_flags = AI_PASSIVE; /* For wildcard IP address */ \
+ hints.ai_protocol = 0; \
+ hints.ai_canonname = NULL; \
+ hints.ai_addr = NULL; \
+ hints.ai_next = NULL; \
+}
+
+static int tcp() {
+ struct addrinfo hints;
+ int sock_fd;
+
+ HINTS_INIT(hints,SOCK_STREAM);
+ sock_fd = listen_sock(&hints);
+ if(sock_fd<0) { return -1; }
+
+ return 1;
+}
+
+static int udp() {
+ struct addrinfo hints;
+ int sock_fd;
+
+ HINTS_INIT(hints,SOCK_DGRAM);
+ sock_fd = listen_sock(&hints);
+ if(sock_fd<0) { return -1; }
+
+ return 1;
+}
+
+
+int start() {
+ if(tcp()<0) { return -1; }
+ if(udp()<0) { return -1; }
+
+ return 1;
+}
+
+static int listen_sock(struct addrinfo *hints) {
+ struct addrinfo *p, *res;
+ int sock_fd;
+
+ if(getaddrinfo(NULL,"5150",hints,&res)!=0) {
+ perror("getaddrinfo");
+ return -1;
+ }
+
+ for(p=res;p!=NULL;p=p->ai_next) {
+ sock_fd = socket(p->ai_family,p->ai_socktype,p->ai_protocol);
+ if(sock_fd<0) { continue; }
+
+ if(bind(sock_fd,p->ai_addr,p->ai_addrlen)==0) { break; /* Success */ }
+
+ close(sock_fd);
+ }
+
+ free(res);
+
+ if(p==NULL) { return -1; }
+
+ if(listen(sock_fd,MAX_BACKLOG)!=0) {
+ perror("listen");
+ return -1;
+ }
+
+ return sock_fd;
+}