Index: trunk/Ohana/src/opihi/lib.shell/SocketOps.c
===================================================================
--- trunk/Ohana/src/opihi/lib.shell/SocketOps.c	(revision 23530)
+++ trunk/Ohana/src/opihi/lib.shell/SocketOps.c	(revision 32632)
@@ -1,3 +1,5 @@
 # include "shell.h"
+# include <unistd.h>
+# define HOST_NAME_MAX 256
 
 # define MY_PORT 2000
@@ -11,34 +13,25 @@
 static int *VALID;
 
-int InitServerSocket (SockAddress *Address) {
-
+int GetPortRange (int *start, int *stop, char *portinfo);
+
+int InitServerSocket (SockAddress *Address, char *hostname, char *portinfo) {
+
+  int start, stop;
   int status, InitSocket, length;
-
-# if (0)
-  struct hostent  *host;
-  char tmpline[80], hostip[80];
-
-  host = gethostbyname (hostname);
-  bzero (hostip, 80);
-  for (i = 0; i < host[0].h_length; i++) {
-    sprintf (tmpline, "%u", (0xff & host[0].h_addr[i]));
-    strcat (hostip, tmpline);
-    if (i < host[0].h_length - 1) strcat (hostip, ".");
-  }
-# endif
-  
+  char myHostname[HOST_NAME_MAX];
+
+  status = gethostname (myHostname, HOST_NAME_MAX);
+
+  fprintf (stderr, "target host: %s, real host: %s\n", hostname, myHostname);
+
+  GetPortRange (&start, &stop, portinfo);
+
+  fprintf (stderr, "using port range %d - %d\n", start, stop);
+
   Address[0].sin_family = AF_INET;
-  Address[0].sin_port   = MY_PORT;
+  Address[0].sin_port   = start;
   Address[0].sin_addr.s_addr = INADDR_ANY; // use this line to bind any address / port?
 
 retry_server:
-
-# if (0)  
-  status = inet_aton (hostip, &Address[0].sin_addr);
-  if (!status) {
-    gprint (GP_ERR, "invalid address\n");
-    exit (4);
-  }
-# endif
 
   length = sizeof(Address[0]);
@@ -74,5 +67,5 @@
     if (errno == EADDRINUSE) {
 	Address[0].sin_port ++;
-	if (Address[0].sin_port > MY_PORT + 10) {
+	if (Address[0].sin_port > stop) {
 	  fprintf (stderr, "failed to find a usable port\n");
 	  exit (6);
@@ -154,7 +147,49 @@
 }
 
-int GetClientSocket (char *hostname) {
-
-  int i, status, InitSocket, length;
+int GetPortRange (int *start, int *stop, char *portinfo) {
+
+  // portinfo is a port or port range of the form NN or NN:MM
+  *start = MY_PORT;
+  *stop = *start + 10;
+  if (!portinfo) return TRUE;
+  if (*portinfo == 0) return TRUE;
+
+  char *endptr;
+  *start = strtol (portinfo, &endptr, 0);
+  *stop = *start + 10; // default range of 10
+  if (!endptr) {
+    gprint (GP_ERR, "error in port range parsing : %s\n", portinfo);
+    exit (20);
+  }
+  if (endptr == portinfo) {
+    gprint (GP_ERR, "error in port range (%s), must be in form NN:MM or NN\n", portinfo);
+    exit (20);
+  }
+  if (*endptr == 0) return TRUE;
+
+  if (*endptr != ':') {
+    gprint (GP_ERR, "error in port range %s (wrong range separator, must be in form NN:MM)\n", portinfo);
+    exit (20);
+  }
+  char *ptr = endptr + 1;
+  *stop = strtol (ptr, &endptr, 0);
+  if (endptr == ptr)  {
+    gprint (GP_ERR, "error in port range (%s), must be in form NN:MM or NN\n", portinfo);
+    exit (20);
+  }
+  if (*stop - *start > 20) {
+    gprint (GP_ERR, "error in port range (%s), range must be 20 or less\n", portinfo);
+    exit (20);
+  }
+  if (*stop < *start) {
+    gprint (GP_ERR, "error in port range (%s), stop must >= start\n", portinfo);
+    exit (20);
+  }
+  return TRUE;
+}
+
+int GetClientSocket (char *hostname, char *portinfo) {
+
+  int i, status, InitSocket, length, start, stop;
   SockAddress Address;
   struct hostent  *host;
@@ -169,10 +204,12 @@
   }
 
+  GetPortRange (&start, &stop, portinfo);
+
   if (DEBUG) {
-    gprint (GP_ERR, "trying %s (%s:%d)...", host[0].h_name, hostip, MY_PORT);
+    gprint (GP_ERR, "trying %s (%s:%d)...", host[0].h_name, hostip, start);
   }
 
   Address.sin_family = AF_INET;
-  Address.sin_port   = MY_PORT;
+  Address.sin_port   = start;
 
 retry_client:
@@ -195,5 +232,5 @@
     if (errno == ECONNREFUSED) {
       Address.sin_port ++;
-      if (Address.sin_port > MY_PORT + 10) exit (12);
+      if (Address.sin_port > stop) exit (12);
       goto retry_client;
     }
