#include <sys/un.h>
 #include <netinet/in.h>
 #include <netdb.h>
+#include <assert.h>
 
 #include "glite/lb/consumer.h"
 #include "glite/lb/context-int.h"
        /* close connection ad free its structures */
        OM_uint32 min_stat;
 
+       assert(ctx->connOpened);
+       assert(conn_index < ctx->connOpened);
+
        edg_wll_gss_close(&ctx->connPool[conn_index].gss, &ctx->p_tmp_timeout);
        if (ctx->connPool[conn_index].gsiCred) 
                gss_release_cred(&min_stat, &ctx->connPool[conn_index].gsiCred);
 int edg_wll_close(edg_wll_Context ctx)
 {
        edg_wll_ResetError(ctx);
+       if (ctx->connToUse == -1) return 0;
 
        CloseConnection(ctx, ctx->connToUse);
                
+       ctx->connToUse = -1;
        return edg_wll_Error(ctx,NULL,NULL);
 }
 
        /* some error occured; close created connection
         * and free all fields in connPool[index] */
        CloseConnection(ctx, index);
+       ctx->connToUse = -1;
 ok:    
        return edg_wll_Error(ctx,NULL,NULL);
 }
                default: goto err;
        }
        
+       assert(ctx->connToUse >= 0);
        gettimeofday(&ctx->connPool[ctx->connToUse].lastUsed, NULL);
        return 0;