Index: trunk/ntripserver/README
===================================================================
--- trunk/ntripserver/README	(revision 598)
+++ trunk/ntripserver/README	(revision 653)
@@ -84,6 +84,14 @@
 - make debug (for debugging purposes).
 
-The exacutable will show up as ntripserver.
-
+To compile the source code on a Windows system where a mingw gcc
+compiler is available, you may like to run the following command:
+
+- gcc -Wall -W -O3 -DWINDOWSVERSION ntripserver.c -DNDEBUG 
+  -o ntripserver -lwsock32, or
+- mingw32-make, or 
+- mingw32-make debug
+
+The exacutable will show up as ntripserver on Linux
+or ntripserver.exe on a Windows system.
 
 Usage
Index: trunk/ntripserver/makefile
===================================================================
--- trunk/ntripserver/makefile	(revision 598)
+++ trunk/ntripserver/makefile	(revision 653)
@@ -1,10 +1,18 @@
 #!/usr/bin/make
-# $Id: makefile,v 1.6 2007/08/30 07:45:59 stoecker Exp $
+# $Id: makefile,v 1.7 2007/08/30 14:57:34 stuerze Exp $
+
+ifdef windir
+CC   = gcc
+OPTS = -Wall -W -O3 -DWINDOWSVERSION
+LIBS = -lwsock32
+else
+OPTS = -Wall -W -O3
+endif
 
 ntripserver: ntripserver.c
-	$(CC) -Wall -W -O3 $? -DNDEBUG -o $@
+	$(CC) $(OPTS) $? -DNDEBUG -o $@ $(LIBS)
 
 debug: ntripserver.c
-	$(CC) -Wall -W -O3 $? -o ntripserver
+	$(CC) $(OPTS) $? -o ntripserver $(LIBS)
 
 clean:
Index: trunk/ntripserver/ntripserver.c
===================================================================
--- trunk/ntripserver/ntripserver.c	(revision 598)
+++ trunk/ntripserver/ntripserver.c	(revision 653)
@@ -1,4 +1,4 @@
 /*
- * $Id: ntripserver.c,v 1.35 2007/08/30 16:40:51 stoecker Exp $
+ * $Id: ntripserver.c,v 1.36 2007/12/14 07:23:44 stoecker Exp $
  *
  * Copyright (c) 2003...2007
@@ -37,6 +37,6 @@
 
 /* CVS revision and version */
-static char revisionstr[] = "$Revision: 1.35 $";
-static char datestr[]     = "$Date: 2007/08/30 16:40:51 $";
+static char revisionstr[] = "$Revision: 1.36 $";
+static char datestr[]     = "$Date: 2007/12/14 07:23:44 $";
 
 #include <ctype.h>
@@ -44,20 +44,39 @@
 #include <fcntl.h>
 #include <getopt.h>
-#include <netdb.h>
-#include <signal.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <sys/time.h>
+#include <sys/types.h>
+#include <time.h>
+#include <signal.h>
 #include <unistd.h>
-#include <arpa/inet.h>
-#include <netinet/in.h>
-#include <sys/socket.h>
-#include <sys/termios.h>
-#include <sys/types.h>
-#include <sys/time.h>
+
+#ifdef WINDOWSVERSION
+  #include <winsock2.h>
+  #include <io.h>
+  #include <sys/stat.h> 
+  #include <windows.h> 
+  typedef SOCKET sockettype;
+  typedef u_long in_addr_t;
+  typedef size_t socklen_t;
+  typedef u_short uint16_t;
+#else
+  typedef int sockettype;
+  #include <arpa/inet.h>
+  #include <sys/socket.h>
+  #include <netinet/in.h>
+  #include <netdb.h>
+  #include <sys/termios.h>
+  #define closesocket(sock) close(sock)
+  #define INVALID_HANDLE_VALUE -1
+  #define INVALID_SOCKET -1
+#endif
 
 #ifndef COMPILEDATE
 #define COMPILEDATE " built " __DATE__
 #endif
+
+#define ALARMTIME (2*60)
 
 #ifndef MSG_DONTWAIT
@@ -85,9 +104,6 @@
 #define NTRIP_PORT      2101
 
-/* default sisnet source */
 #define SISNET_SERVER   "131.176.49.142"
 #define SISNET_PORT     7777
-
-#define ALARMTIME       60
 
 #define RTP_VERSION     2
@@ -99,29 +115,38 @@
 static enum MODE inputmode     = INFILE;
 static int sisnet              = 31;
-static int gpsfd               = -1;
-static int socket_tcp          = -1;
-static int socket_udp          = -1;
+static int gps_file            = -1;
+static sockettype gps_socket   = INVALID_SOCKET;
+static sockettype socket_tcp   = INVALID_SOCKET;
+static sockettype socket_udp   = INVALID_SOCKET;
+#ifndef WINDOWSVERSION
+static int gps_serial          = INVALID_HANDLE_VALUE;
+static int sigpipe_received    = 0;
+#else
+HANDLE gps_serial              = INVALID_HANDLE_VALUE;
+#endif
+static int sigalarm_received   = 0;
 static int sigint_received     = 0;
-static int sigalarm_received   = 0;
-static int sigpipe_received    = 0;
 static int reconnect_sec       = 1;
 
 
 /* Forward references */
-static int  openserial(const char * tty, int blocksz, int baud);
-static void send_receive_loop(int sock, int fd, int outmode,
+static void send_receive_loop(sockettype sock, int outmode,
   struct sockaddr * pcasterRTP, socklen_t length);
 static void usage(int, char *);
 static int  encode(char *buf, int size, const char *user, const char *pwd);
-static int  send_to_caster(char *input, int socket, int input_size);
+static int  send_to_caster(char *input, sockettype socket, int input_size);
 static void close_session(const char *caster_addr, const char *mountpoint, 
   int cseq, int session, char *rtsp_ext, int fallback);
 static int  reconnect(int rec_sec, int rec_sec_max);
-
-/* Signal Handling */
 static void handle_sigint(int sig);
+static void setup_signal_handler(int sig, void (*handler)(int));
+#ifndef WINDOWSVERSION
+static int  openserial(const char * tty, int blocksz, int baud);
+static void handle_sigpipe(int sig);
 static void handle_alarm(int sig);
-static void handle_sigpipe(int sig);
-static void setup_signal_handler(int sig, void (*handler)(int));
+#else
+static HANDLE openserial(const char * tty, int baud);
+#endif 
+
 
 /*
@@ -230,14 +255,22 @@
   }
 
+  /* setup signal handler for CTRL+C */
+  setup_signal_handler(SIGINT, handle_sigint);
+#ifndef WINDOWSVERSION 
+  /* setup signal handler for boken pipe */
+  setup_signal_handler(SIGPIPE, handle_sigpipe);
   /* setup signal handler for timeout */
   setup_signal_handler(SIGALRM, handle_alarm);
   alarm(ALARMTIME);
-
-  /* setup signal handler for CTRL+C */
-  setup_signal_handler(SIGINT, handle_sigint);
+#else
+  /* winsock initialization */
+  WSADATA wsaData;
+  if (WSAStartup(MAKEWORD(1,1), &wsaData))
+  {
+    fprintf(stderr, "Could not init network access.\n");
+    return 20;
+  }
+#endif
   
-/* setup signal handler for boken pipe */
-  setup_signal_handler(SIGPIPE, handle_sigpipe);
-
   /* get and check program arguments */
   if(argc <= 1)
@@ -497,4 +530,5 @@
   {
     int input_init = 1;
+    if(sigint_received) break;
     /*** InputMode handling ***/
     switch(inputmode)
@@ -502,12 +536,14 @@
     case INFILE:
       {
-	if((gpsfd = open(filepath, O_RDONLY)) < 0)
+	if((gps_file = open(filepath, O_RDONLY)) < 0)
 	{
           perror("ERROR: opening input file");
           exit(1);
 	}
+#ifndef WINDOWSVERSION
 	/* set blocking inputmode in case it was not set
           (seems to be sometimes for fifo's) */
-	fcntl(gpsfd, F_SETFL, 0);
+	fcntl(gps_file, F_SETFL, 0);
+#endif
 	printf("file input: file = %s\n", filepath);
       }
@@ -515,9 +551,10 @@
     case SERIAL: /* open serial port */
       {
-	gpsfd = openserial(ttyport, 1, ttybaud);
-	if(gpsfd < 0)
-	{
-          exit(1);
-	}
+#ifndef WINDOWSVERSION
+	gps_serial = openserial(ttyport, 1, ttybaud);
+#else
+	gps_serial = openserial(ttyport, ttybaud);
+#endif
+	if(gps_serial == INVALID_HANDLE_VALUE) exit(1);
 	printf("serial input: device = %s, speed = %d\n", ttyport, ttybaud);
       }
@@ -547,6 +584,6 @@
 	}
 
-	if((gpsfd = socket(AF_INET, inputmode == UDPSOCKET
-	? SOCK_DGRAM : SOCK_STREAM, 0)) < 0)
+	if((gps_socket = socket(AF_INET, inputmode == UDPSOCKET
+	? SOCK_DGRAM : SOCK_STREAM, 0)) == INVALID_SOCKET)
 	{
           fprintf(stderr,
@@ -571,5 +608,5 @@
 	if(bindmode)
 	{
-          if(bind(gpsfd, (struct sockaddr *) &caster, sizeof(caster)) < 0)
+          if(bind(gps_socket, (struct sockaddr *) &caster, sizeof(caster)) < 0)
           {
             fprintf(stderr, "ERROR: can't bind input to port %d\n", inport);
@@ -579,5 +616,5 @@
           }
 	} /* connect to input-caster or proxy server*/
-	else if(connect(gpsfd, (struct sockaddr *)&caster, sizeof(caster)) < 0)
+	else if(connect(gps_socket, (struct sockaddr *)&caster, sizeof(caster)) < 0)
 	{
           fprintf(stderr, "WARNING: can't connect input to %s at port %d\n",
@@ -592,5 +629,5 @@
 
           /* set socket buffer size */
-          setsockopt(gpsfd, SOL_SOCKET, SO_SNDBUF, (const char *) &size,
+          setsockopt(gps_socket, SOL_SOCKET, SO_SNDBUF, (const char *) &size,
             sizeof(const char *));
           if(stream_user && stream_password)
@@ -634,5 +671,5 @@
             "\r\n", get_extension, stream_name, AGENTSTRING, revisionstr);
           }
-          if((send(gpsfd, szSendBuffer, (size_t)nBufferBytes, 0))
+          if((send(gps_socket, szSendBuffer, (size_t)nBufferBytes, 0))
           != nBufferBytes)
           {
@@ -644,5 +681,5 @@
           /* check Source caster's response */
           while(!init && nBufferBytes < (int)sizeof(szSendBuffer)
-          && (nBufferBytes += recv(gpsfd, szSendBuffer,
+          && (nBufferBytes += recv(gps_socket, szSendBuffer,
           sizeof(szSendBuffer)-nBufferBytes, 0)) > 0)
           {
@@ -683,5 +720,5 @@
             while((i = fread(buffer, 1, sizeof(buffer), fh)) > 0)
             {
-              if((send(gpsfd, buffer, (size_t)i, 0)) != i)
+              if((send(gps_socket, buffer, (size_t)i, 0)) != i)
               {
         	perror("WARNING: sending init file");
@@ -715,5 +752,5 @@
 	i = snprintf(buffer, sizeof(buffer), sisnet >= 30 ? "AUTH,%s,%s\r\n"
           : "AUTH,%s,%s", sisnetuser, sisnetpassword);
-	if((send(gpsfd, buffer, (size_t)i, 0)) != i)
+	if((send(gps_socket, buffer, (size_t)i, 0)) != i)
 	{
           perror("WARNING: sending authentication for SISNeT data server");
@@ -722,5 +759,5 @@
 	}
 	i = sisnet >= 30 ? 7 : 5;
-	if((j = recv(gpsfd, buffer, i, 0)) != i && strncmp("*AUTH", buffer, 5))
+	if((j = recv(gps_socket, buffer, i, 0)) != i && strncmp("*AUTH", buffer, 5))
 	{
           fprintf(stderr, "WARNING: SISNeT connect failed:");
@@ -738,5 +775,5 @@
 	if(sisnet >= 31)
 	{
-          if((send(gpsfd, "START\r\n", 7, 0)) != i)
+          if((send(gps_socket, "START\r\n", 7, 0)) != i)
           {
             perror("WARNING: sending Sisnet start command");
@@ -760,8 +797,8 @@
 	{
           fprintf(stderr, "Sending user ID for receiver...\n");
-          nBufferBytes = read(gpsfd, szSendBuffer, BUFSZ);
+          nBufferBytes = recv(gps_socket, szSendBuffer, BUFSZ, 0);
           strcpy(szSendBuffer, recvrid);
           strcat(szSendBuffer,"\r\n");
-          if(send(gpsfd,szSendBuffer, strlen(szSendBuffer), MSG_DONTWAIT) < 0)
+          if(send(gps_socket,szSendBuffer, strlen(szSendBuffer), MSG_DONTWAIT) < 0)
           {
             perror("WARNING: sending user ID for receiver");
@@ -781,8 +818,8 @@
 	{
           fprintf(stderr, "Sending user password for receiver...\n");
-          nBufferBytes = read(gpsfd, szSendBuffer, BUFSZ);
+          nBufferBytes = recv(gps_socket, szSendBuffer, BUFSZ, 0);
           strcpy(szSendBuffer, recvrpwd);
           strcat(szSendBuffer,"\r\n");
-          if(send(gpsfd, szSendBuffer, strlen(szSendBuffer), MSG_DONTWAIT) < 0)
+          if(send(gps_socket, szSendBuffer, strlen(szSendBuffer), MSG_DONTWAIT) < 0)
           {
             perror("WARNING: sending user password for receiver");
@@ -803,6 +840,9 @@
     while((input_init) && (output_init))
     {
-      if((sigint_received) || (sigalarm_received) || (sigpipe_received)) break;
-
+#ifndef WINDOWSVERSION
+      if((sigalarm_received) || (sigint_received) || (sigpipe_received)) break;
+#else
+      if((sigalarm_received) || (sigint_received)) break;
+#endif
       if(!(he = gethostbyname(outhost)))
       {
@@ -814,5 +854,5 @@
 
       /* create socket */
-      if((socket_tcp = socket(AF_INET, SOCK_STREAM, 0)) < 0)
+      if((socket_tcp = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
       {
 	perror("ERROR: tcp socket");
@@ -884,5 +924,6 @@
           }
 #endif
-          send_receive_loop(socket_tcp, gpsfd, outputmode, NULL, 0);
+          send_receive_loop(socket_tcp, outputmode, NULL, 0);
+          input_init = output_init = 0;
           break;
 	case HTTP: /*** Ntrip-Version 2.0 HTTP/1.1 ***/
@@ -952,8 +993,9 @@
           }
 #endif
-          send_receive_loop(socket_tcp, gpsfd, outputmode, NULL, 0);
+          send_receive_loop(socket_tcp, outputmode, NULL, 0);
+          input_init = output_init = 0;
           break;
 	case RTSP: /*** Ntrip-Version 2.0 RTSP / RTP ***/
-          if((socket_udp = socket(AF_INET, SOCK_DGRAM,0)) < 0)
+          if((socket_udp = socket(AF_INET, SOCK_DGRAM,0)) == INVALID_SOCKET)
           {
             perror("ERROR: udp socket");
@@ -1087,5 +1129,5 @@
               || (nBufferBytes < 0))
               {
-        	fprintf(stderr, "ERROR: Destination caster request to long\n");
+        	    fprintf(stderr, "ERROR: Destination caster request to long\n");
                 reconnect_sec_max = 0;
                 output_init = 0;
@@ -1094,6 +1136,6 @@
               if(!send_to_caster(szSendBuffer, socket_tcp, nBufferBytes))
               {
-                 output_init = 0;
-        	 break;
+                output_init = 0;
+        	    break;
               }
             }
@@ -1119,5 +1161,5 @@
               cseq = 2;
               len = (socklen_t)sizeof(casterRTP);
-              send_receive_loop(socket_udp, gpsfd, outputmode, (struct sockaddr *)&casterRTP, 
+              send_receive_loop(socket_udp, outputmode, (struct sockaddr *)&casterRTP, 
               (socklen_t)len);
               break;
@@ -1125,12 +1167,11 @@
             else{break;}
           }
+          input_init = output_init = 0;
           break;
       }     
     }
     close_session(casterouthost, mountpoint, cseq, session, rtsp_extension, 0);
-    if((!sigint_received) && (reconnect_sec_max))
-    {
-       reconnect_sec = reconnect(reconnect_sec, reconnect_sec_max);
-    }
+    if((reconnect_sec_max)  && (!sigint_received))
+      reconnect_sec = reconnect(reconnect_sec, reconnect_sec_max);
     else inputmode = LAST;
   }
@@ -1138,34 +1179,57 @@
 }
 
-static void send_receive_loop(int sock, int fd, int outmode, struct sockaddr* pcasterRTP,
+static void send_receive_loop(sockettype sock, int outmode, struct sockaddr* pcasterRTP,
 socklen_t length)
 {
-  int nodata = 0;
-  char buffer[BUFSZ] = { 0 };
-  char sisnetbackbuffer[200];
-  char szSendBuffer[BUFSZ] = "";
-  int nBufferBytes = 0;
+  int      nodata = 0;
+  char     buffer[BUFSZ] = { 0 };
+  char     sisnetbackbuffer[200];
+  char     szSendBuffer[BUFSZ] = "";
+  int      nBufferBytes = 0;
 
    /* RTSP / RTP Mode */
-  int    isfirstpacket = 1;
-  struct timeval now;
-  struct timeval last = {0,0};
+  int      isfirstpacket = 1;
+  struct   timeval now;
+  struct   timeval last = {0,0};
   long int sendtimediff;
-  int rtpseq = 0;
-  int rtpssrc = 0;
-  int rtptime = 0;
+  int      rtpseq = 0;
+  int      rtpssrc = 0;
+  int      rtptime = 0;
 
   /* data transmission */
   fprintf(stderr,"transfering data ...\n");
-  int send_recv_success = 0;
+  int  send_recv_success = 0;
+#ifdef WINDOWSVERSION 
+  time_t nodata_begin = 0, nodata_current = 0;  
+#endif
   while(1)
   {
     if(send_recv_success < 3) send_recv_success++;  
-    if(!nodata) alarm(ALARMTIME);
-    else nodata = 0; 
-
+    if(!nodata)
+    { 
+#ifndef WINDOWSVERSION
+      alarm(ALARMTIME);
+#else
+      time(&nodata_begin);
+#endif
+    }
+    else 
+    {
+      nodata = 0; 
+#ifdef WINDOWSVERSION 
+      time(&nodata_current);
+      if(difftime(nodata_current, nodata_begin) >= ALARMTIME) 
+      {
+        sigalarm_received = 1;
+        fprintf(stderr, "ERROR: more than %d seconds no activity\n", ALARMTIME);
+      }
+#endif
+    }
     /* signal handling*/
-    if((sigint_received) || (sigpipe_received) || (sigalarm_received)) break;
-    
+#ifdef WINDOWSVERSION
+    if((sigalarm_received) || (sigint_received)) break;
+#else
+    if((sigalarm_received) || (sigint_received) || (sigpipe_received)) break;
+#endif
     if(!nBufferBytes)
     {
@@ -1179,5 +1243,5 @@
         memcpy(sisnetbackbuffer, buffer, sizeof(sisnetbackbuffer));
         i = (sisnet >= 30 ? 5 : 3);
-        if((send(gpsfd, "MSG\r\n", i, 0)) != i)
+        if((send(gps_socket, "MSG\r\n", i, 0)) != i)
         {
           perror("WARNING: sending SISNeT data request failed");
@@ -1186,10 +1250,31 @@
       }
       /*** receiving data ****/
-      nBufferBytes = read(fd, buffer, sizeof(buffer));
+      if(inputmode == INFILE)
+        nBufferBytes = read(gps_file, buffer, sizeof(buffer));
+      else if(inputmode == SERIAL)
+      {
+#ifndef WINDOWSVERSION        
+        nBufferBytes = read(gps_serial, buffer, sizeof(buffer));
+#else
+        DWORD nRead = 0;  
+        if(!ReadFile(gps_serial, buffer, sizeof(buffer), &nRead, NULL))
+        {
+          fprintf(stderr,"ERROR: reading serial input failed\n");
+          return;
+        }
+        nBufferBytes = (int)nRead;
+#endif
+      }
+      else 
+        nBufferBytes = recv(gps_socket, buffer, sizeof(buffer), 0);
       if(!nBufferBytes)
       {
-        printf("WARNING: no data received from input\n");
+        fprintf(stderr, "WARNING: no data received from input\n");
+        nodata = 1;
+#ifndef WINDOWSVERSION
         sleep(3);
-        nodata = 1;
+#else
+        Sleep(3*1000);
+#endif
         continue;
       }
@@ -1340,27 +1425,29 @@
  *     tty     : pointer to    : A zero-terminated string containing the device
  *               unsigned char   name of the appropriate serial port.
- *     blocksz : integer       : Block size for port I/O
+ *     blocksz : integer       : Block size for port I/O  (ifndef WINDOWSVERSION)
  *     baud :    integer       : Baud rate for port I/O
  *
  * Return Value:
  *     The function returns a file descriptor for the opened port if successful.
- *     The function returns -1 in the event of an error.
+ *     The function returns -1 / INVALID_HANDLE_VALUE in the event of an error.
  *
  * Remarks:
  *
  ********************************************************************/
-
+#ifndef WINDOWSVERSION
 static int openserial(const char * tty, int blocksz, int baud)
 {
-  int fd;
   struct termios termios;
 
-  fd = open(tty, O_RDWR | O_NONBLOCK | O_EXLOCK);
-  if(fd < 0)
+/*** opening the serial port ***/
+  gps_serial = open(tty, O_RDWR | O_NONBLOCK | O_EXLOCK);
+  if(gps_serial < 0)
   {
     perror("ERROR: opening serial connection");
     return (-1);
   }
-  if(tcgetattr(fd, &termios) < 0)
+
+/*** configuring the serial port ***/
+  if(tcgetattr(gps_serial, &termios) < 0)
   {
     perror("ERROR: get serial attributes");
@@ -1381,5 +1468,4 @@
 #if (B4800 != 4800)
 /* Not every system has speed settings equal to absolute speed value. */
-
   switch (baud)
   {
@@ -1437,15 +1523,110 @@
     return (-1);
   }
-  if(tcsetattr(fd, TCSANOW, &termios) < 0)
+  if(tcsetattr(gps_serial, TCSANOW, &termios) < 0)
   {
     perror("ERROR: setting serial attributes");
     return (-1);
   }
-  if(fcntl(fd, F_SETFL, 0) == -1)
+  if(fcntl(gps_serial, F_SETFL, 0) == -1)
   {
     perror("WARNING: setting blocking inputmode failed");
   }
-  return (fd);
+  return (gps_serial);
+}
+#else
+static HANDLE openserial(const char * tty, int baud)
+{
+  DCB dcb;  
+  COMMTIMEOUTS cmt;
+
+/*** opening the serial port ***/
+  gps_serial = CreateFile(tty, GENERIC_READ | GENERIC_WRITE, 0, 0, 
+    OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
+  if(gps_serial == INVALID_HANDLE_VALUE)
+  {
+    fprintf(stderr, "ERROR: opening serial connection\n");
+    return (INVALID_HANDLE_VALUE);
+  }
+/*** configuring the serial port ***/
+  FillMemory(&dcb, sizeof(dcb), 0);
+  dcb.DCBlength = sizeof(dcb);
+  if(!GetCommState(gps_serial, &dcb))
+  {
+    fprintf(stderr, "ERROR: get serial attributes\n");
+    return (INVALID_HANDLE_VALUE);
+  }
+  switch (baud)
+  {
+  case 110:
+    baud = CBR_110;
+    break;
+  case 300:
+    baud = CBR_300;
+    break;
+  case 600:
+    baud = CBR_600;
+    break;
+  case 1200:
+    baud = CBR_1200;
+    break;
+  case 2400:
+    baud = CBR_2400;
+    break;
+  case 4800:
+    baud = CBR_4800;
+    break;
+  case 9600:
+    baud = CBR_9600;
+    break;
+  case 14400:
+    baud = CBR_14400;
+    break;
+  case 19200:
+    baud = CBR_19200;
+    break;
+  case 38400:
+    baud = CBR_38400;
+    break;
+  case 56000:
+    baud = CBR_56000;
+    break;
+  case 57600:
+    baud = CBR_57600;
+    break;
+  case 115200:
+    baud = CBR_115200;
+    break;
+  case 128000:
+    baud = CBR_128000;
+    break;
+  case 256000:
+    baud = CBR_256000;
+    break;
+  default:
+    fprintf(stderr, "WARNING: Baud settings not useful, using 19200\n");
+    baud = CBR_19200;
+    break;
+  }
+  dcb.BaudRate = baud; 
+  dcb.ByteSize = 8;
+  dcb.StopBits = ONESTOPBIT;
+  dcb.Parity   = NOPARITY;
+  if(!GetCommState(gps_serial, &dcb))
+  {
+    fprintf(stderr, "ERROR: get serial attributes\n");
+    return (INVALID_HANDLE_VALUE);
+  }
+  FillMemory(&cmt, sizeof(cmt), 0);
+  cmt.ReadIntervalTimeout = 1000;
+  cmt.ReadTotalTimeoutMultiplier = 1;
+  cmt.ReadTotalTimeoutConstant = 0;
+  if(!SetCommTimeouts(gps_serial, &cmt))
+  {
+    fprintf(stderr, "ERROR: set serial timeouts\n");
+    return (INVALID_HANDLE_VALUE); 
+  }
+  return (gps_serial);
 } /* openserial */
+#endif
 
 /********************************************************************
@@ -1463,5 +1644,4 @@
 *
 *********************************************************************/
-static
 #ifdef __GNUC__
 __attribute__ ((noreturn))
@@ -1570,4 +1750,5 @@
 }
 
+#ifndef WINDOWSVERSION
 #ifdef __GNUC__
 static void handle_alarm(int sig __attribute__((__unused__)))
@@ -1585,5 +1766,8 @@
 static void handle_sigpipe(int sig)
 #endif /* __GNUC__ */
-{sigpipe_received = 1;}
+{
+  sigpipe_received = 1;
+}
+#endif /* WINDOWSVERSION */
 
 static void setup_signal_handler(int sig, void (*handler)(int))
@@ -1602,4 +1786,5 @@
   return;
 } /* setupsignal_handler */
+
 
 /********************************************************************
@@ -1660,5 +1845,5 @@
  * send message to caster                                           *
 *********************************************************************/
-static int send_to_caster(char *input, int socket, int input_size)
+static int send_to_caster(char *input, sockettype socket, int input_size)
 {
  int send_error = 1;
@@ -1688,7 +1873,11 @@
   rec_sec *= 2;
   if (rec_sec > rec_sec_max) rec_sec = rec_sec_max;
+#ifndef WINDOWSVERSION
   sleep(rec_sec);
+  sigpipe_received = 0;
+#else
+  Sleep(rec_sec*1000);
+#endif
   sigalarm_received = 0;
-  sigpipe_received = 0;
   return rec_sec;
 } /* reconnect */
@@ -1704,21 +1893,64 @@
   char send_buf[BUFSZ];
 
-  if((gpsfd != -1) && (!fallback))
-  {
-    if(close(gpsfd) == -1)
-    {
-      perror("ERROR: close input device ");
-      exit(0);
-    }
-    else
-    {
-      gpsfd = -1;
+  if(!fallback)
+  {
+    if((gps_socket != INVALID_SOCKET) && 
+       ((inputmode == TCPSOCKET) || (inputmode == UDPSOCKET) || 
+       (inputmode == CASTER)    || (inputmode == SISNET)))
+    { 
+      if(closesocket(gps_socket) == -1)
+      {
+        perror("ERROR: close input device ");
+        exit(0);
+      }
+      else
+      {
+        gps_socket = -1;
 #ifndef NDEBUG  
-    fprintf(stderr, "close input device: successful\n");
-#endif
-    }
-  }
-
-  if(socket_udp  != -1)
+        fprintf(stderr, "close input device: successful\n");
+#endif
+      }
+    }
+    else if((gps_serial != INVALID_HANDLE_VALUE) && (inputmode == SERIAL))
+    {
+#ifndef WINDOWSVERSION
+      if(close(gps_serial) == INVALID_HANDLE_VALUE)
+      {
+        perror("ERROR: close input device ");
+        exit(0);
+      }
+#else
+      if(!CloseHandle(gps_serial))
+      {
+        fprintf(stderr, "ERROR: close input device ");
+        exit(0);
+      }
+#endif
+      else
+      {
+        gps_serial = INVALID_HANDLE_VALUE;
+#ifndef NDEBUG  
+        fprintf(stderr, "close input device: successful\n");
+#endif
+      }
+    }
+    else if((gps_file != -1) && (inputmode == INFILE))
+    { 
+      if(close(gps_file) == -1)
+      {
+        perror("ERROR: close input device ");
+        exit(0);
+      }
+      else
+      {
+        gps_file = -1;
+#ifndef NDEBUG  
+        fprintf(stderr, "close input device: successful\n");
+#endif
+      }
+    }
+  }
+
+  if(socket_udp  != INVALID_SOCKET)
   {
     if(cseq == 2)
@@ -1742,5 +1974,5 @@
 #endif
     }
-    if(close(socket_udp)==-1)
+    if(closesocket(socket_udp)==-1)
     {
       perror("ERROR: close udp socket");
@@ -1756,7 +1988,7 @@
   }
 
-  if(socket_tcp != -1)
-  {
-    if(close(socket_tcp) == -1)
+  if(socket_tcp != INVALID_SOCKET)
+  {
+    if(closesocket(socket_tcp) == -1)
     {
       perror("ERROR: close tcp socket");
