--- z3950.module	2007-09-08 13:50:39.000000000 +0200
+++ z3950.module-neu	2009-06-19 13:05:17.000000000 +0200
@@ -12,6 +12,12 @@
  * See http://www.loc.gov/z3950/lcserver.html
  */
 define('YAZ_DEFAULT_OPTON', '1.2.840.10003.5.10');
+
+// added difinitions got from http://www.loc.gov/z3950/agency/defns/oids.html#5
+define('YAZ_SYNTAX_MARC21', '1.2.840.10003.5.10');
+define('YAZ_SYNTAX_MAB', '1.2.840.10003.5.16');		// MAB2 not in list
+define('YAZ_DEFAULT_SYNTAX', YAZ_SYNTAX_MARC21);
+
 define('YAZ_DEFAULT_SERVER', 'z3950.loc.gov:7090/Voyager'); // jenson.stanford.edu:2210/unicorn
 
 /**
@@ -139,6 +145,44 @@
   }
 }
 
+
+/**
+ * internal: split server line into components
+ */
+function _z3950_serverline_split ($serverline,&$server=NULL,&$options=array(),&$domain=NULL,&$port=NULL,&$db=NULL) {
+  if (! $serverline)	return 0;
+
+  // separate list of options from server 
+  //	opt1=val1,opt2=val2:server ... :
+  preg_match('/^(([^:]+):)?((([^:.\/]+)([.][^:.\/]+)+)(:([0-9]+))?(\/([^:\/]+))?)$/',
+		$serverline,$server_args);
+  //  drupal_set_message('options='.$server_args[2], 'info');
+  //  drupal_set_message('domain='.$server_args[4], 'info');
+  //  drupal_set_message('port='.$server_args[8], 'info');
+  //  drupal_set_message('db='.$server_args[10], 'info');
+
+  $server = $server_args[3];
+  $domain = $server_args[4];
+  $port = $server_args[8];
+  $db = $server_args[10];
+  if ( isset($options))	{
+    $optionlist = explode(',', $server_args[2]);
+
+    foreach ($optionlist as $option) {
+      preg_match('/^([^=]+)=(.*)/',$option, $optionparts);
+      if ($optionparts[2]) {
+        $options[$optionparts[1]] = $optionparts[2];
+      } else
+      { // option name without value like "tcp"
+        // push it back to front of server
+      }
+    }
+  }
+
+  return 1;
+}
+
+
 /**
  * internal implementation of hook_search op=$search
  */
@@ -172,11 +216,16 @@
     // setup the YAZ search on each server
     $ids = array();
     foreach ($servers as $server_index => $server) {
+      _z3950_serverline_split ($server,$server,$options);
       // connect to the yaz server
-      $id = $ids[$server_index] = yaz_connect($server);
+      if ( !($id = $ids[$server_index] = yaz_connect($server, $options)) ) {
+        drupal_set_message('z39.50: connection failed to "' . $server.'"', 'error');
+        continue;
+      }
 
       // tell the yaz server the preferred syntax
       yaz_syntax($id, variable_get('z3950_syntax', YAZ_DEFAULT_SYNTAX));
+      //yaz_syntax($id, variable_get('z3950_syntax', YAZ_SYNTAX_MAB));
 
       // parse the query and setup the search
       yaz_ccl_conf($id, $fields);
@@ -191,7 +240,7 @@
         if (count($servers) > 1) {
           $error .= '('. $servers[$server_index] .')';
         }
-        drupal_set_message($error, 'error');
+        drupal_set_message('yaz_ccl_parse: ' .$error, 'error');
 
         // on failure, remove this server from the waiting list
         unset($ids[$server_index]);
@@ -223,9 +272,8 @@
               // set the starting position
               $server_pos[$server_index] = 1;
 
-              // parse this server:port/database into it's three parts
-              preg_match('/^([^\/|^:]+):*([0-9]*)\/*(.*)/i', $servers[$server_index], $zurl_parts);
-              $server_part = $zurl_parts[1];
+              // parse this options:server:port/database into it's parts
+              _z3950_serverline_split ($server,$server,$options,$server_part);
               if ($server_part == 'localhost' or $server_part == '127.0.0.1') {
                 $server_part = $_SERVER['SERVER_NAME'];
               }
