+// Disassociate connection from client
+static void connection_release(struct connection *co)
+{
+ struct client *cl = co->client;
+
+ yaz_log(YLOG_DEBUG, "Connection release %s", co->host->hostport);
+ if (!cl)
+ return;
+ cl->connection = 0;
+ co->client = 0;
+}
+
+// Close connection and recycle structure
+static void connection_destroy(struct connection *co)
+{
+ struct host *h = co->host;
+ cs_close(co->link);
+ iochan_destroy(co->iochan);
+
+ yaz_log(YLOG_DEBUG, "Connection destroy %s", co->host->hostport);
+ if (h->connections == co)
+ h->connections = co->next;
+ else
+ {
+ struct connection *pco;
+ for (pco = h->connections; pco && pco->next != co; pco = pco->next)
+ ;
+ if (pco)
+ pco->next = co->next;
+ else
+ abort();
+ }
+ if (co->client)
+ {
+ if (co->client->state != Client_Idle)
+ co->client->state = Client_Disconnected;
+ co->client->connection = 0;
+ }
+ co->next = connection_freelist;
+ connection_freelist = co;
+}
+
+// Creates a new connection for client, associated with the host of
+// client's database
+static struct connection *connection_create(struct client *cl)
+{
+ struct connection *new;
+ COMSTACK link;
+ int res;
+ void *addr;
+
+ yaz_log(YLOG_DEBUG, "Connection create %s", cl->database->url);
+ if (!(link = cs_create(tcpip_type, 0, PROTO_Z3950)))
+ {
+ yaz_log(YLOG_FATAL|YLOG_ERRNO, "Failed to create comstack");
+ exit(1);
+ }
+
+ if (!(addr = cs_straddr(link, cl->database->host->ipport)))
+ {
+ yaz_log(YLOG_WARN|YLOG_ERRNO, "Lookup of IP address failed?");
+ return 0;
+ }
+
+ res = cs_connect(link, addr);
+ if (res < 0)
+ {
+ yaz_log(YLOG_WARN|YLOG_ERRNO, "cs_connect %s", cl->database->url);
+ return 0;
+ }
+
+ if ((new = connection_freelist))
+ connection_freelist = new->next;
+ else
+ {
+ new = xmalloc(sizeof (struct connection));
+ new->ibuf = 0;
+ new->ibufsize = 0;
+ }
+ new->state = Conn_Connecting;
+ new->host = cl->database->host;
+ new->next = new->host->connections;
+ new->host->connections = new;
+ new->client = cl;
+ cl->connection = new;
+ new->link = link;
+
+ new->iochan = iochan_create(cs_fileno(link), handler, 0);
+ iochan_setdata(new->iochan, new);
+ new->iochan->next = channel_list;
+ channel_list = new->iochan;
+ return new;
+}
+
+// Close connection and set state to error
+static void client_fatal(struct client *cl)