#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <syslog.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <sys/timeb.h>
#include <sys/param.h>
#include <netinet/in.h>
#include <netdb.h>



int so_open(port) 
     u_short port;
{
  int s;
  int on = 1;
  struct sockaddr_in saddr;
  struct hostent *he;
  char name[MAXHOSTNAMELEN+1];

  bzero(&saddr, sizeof(saddr));
  gethostname(name, MAXHOSTNAMELEN);
  he = gethostbyname(name);
  saddr.sin_family = he->h_addrtype;
  saddr.sin_port = htons(port);
  
  s = socket(AF_INET, SOCK_STREAM, 0);
  if(s == -1) {
    syslog(LOG_ALERT, "socket: %m");
    exit(1);
  }

  if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) {
    syslog(LOG_ALERT, "setsockopt: %m");
    exit(1);
  }

  bind(s, &saddr, sizeof(saddr));
  listen(s, 10);
  return(s);

}

int so_next_dude(s)
     int s;
{
  struct sockaddr_in saddr;
  int i;
  
  i = sizeof(saddr);
  getsockname(s, &saddr, &i);
  
  return accept(s, &saddr, &i);
}

