--- C:\Users\m\Desktop\psqlodbc-09.03.0210\connection.c	2014-03-06 15:48:44.000000000 +0200
+++ C:\Users\m\Desktop\psqlodbc-09.03.0210_fixed\connection.c	2014-04-06 22:47:59.000000000 +0200
@@ -2743,13 +2743,13 @@
 	CSTR	per_query_svp = "_per_query_svp_";
 	CSTR	rlscmd = "RELEASE";
 	static size_t	lenbgncmd = 0, lenrbkcmd = 0, lensvpcmd = 0,
 			lenrlscmd = 0, lenperqsvp = 0;
 	size_t	qrylen;
 	int			id;
-	SocketClass *sock = self->sock;
+	//SocketClass *sock = self->sock;
 	int			maxlen,
 				empty_reqs;
 	BOOL		ReadyToReturn = FALSE,
 				query_completed = FALSE,
 				beforeV2 = PG_VERSION_LT(self, 6.4),
 				aborted = FALSE,
@@ -2812,13 +2812,13 @@
 	if ((NULL == query) || (query[0] == '\0'))
 	{
 		CLEANUP_FUNC_CONN_CS(func_cs_count, self);
 		return NULL;
 	}
 
-	if (SOCK_get_errcode(sock) != 0)
+	if (SOCK_get_errcode(self->sock) != 0)
 	{
 		CC_set_error(self, CONNECTION_COULD_NOT_SEND, "Could not send Query to backend", func);
 		CC_on_abort(self, CONN_DEAD);
 		CLEANUP_FUNC_CONN_CS(func_cs_count, self);
 		return NULL;
 	}
@@ -2866,14 +2866,14 @@
 					goto cleanup;
 				}
 			}
 		}
 	}
 
-	SOCK_put_char(sock, 'Q');
-	if (SOCK_get_errcode(sock) != 0)
+	SOCK_put_char(self->sock, 'Q');
+	if (SOCK_get_errcode(self->sock) != 0)
 	{
 		CC_set_error(self, CONNECTION_COULD_NOT_SEND, "Could not send Query to backend", func);
 		goto cleanup;
 	}
 	if (stmt)
 		SC_forget_unnamed(stmt);
@@ -2896,46 +2896,46 @@
 		if (query_rollback)
 		{
 			leng += (UInt4) (lensvpcmd + 1 + lenperqsvp + 1);
 			leng += (UInt4) (1 + lenrlscmd + 1 + lenperqsvp);
 		}
 		leng++;
-		SOCK_put_int(sock, leng + 4, 4);
+		SOCK_put_int(self->sock, leng + 4, 4);
 inolog("leng=%d\n", leng);
 	}
 	if (issue_begin)
 	{
-		SOCK_put_n_char(sock, bgncmd, lenbgncmd);
-		SOCK_put_n_char(sock, semi_colon, 1);
+		SOCK_put_n_char(self->sock, bgncmd, lenbgncmd);
+		SOCK_put_n_char(self->sock, semi_colon, 1);
 		discard_next_begin = TRUE;
 	}
 	if (query_rollback)
 	{
 		char cmd[64];
 
 		snprintf(cmd, sizeof(cmd), "%s %s;", svpcmd, per_query_svp);
-		SOCK_put_n_char(sock, cmd, strlen(cmd));
+		SOCK_put_n_char(self->sock, cmd, strlen(cmd));
 		discard_next_savepoint = TRUE;
 	}
-	SOCK_put_n_char(sock, query, qrylen);
+	SOCK_put_n_char(self->sock, query, qrylen);
 	if (appendq)
 	{
-		SOCK_put_n_char(sock, semi_colon, 1);
-		SOCK_put_n_char(sock, appendq, strlen(appendq));
+		SOCK_put_n_char(self->sock, semi_colon, 1);
+		SOCK_put_n_char(self->sock, appendq, strlen(appendq));
 	}
 	if (query_rollback)
 	{
 		char cmd[64];
 
 		snprintf(cmd, sizeof(cmd), ";%s %s", rlscmd, per_query_svp);
-		SOCK_put_n_char(sock, cmd, strlen(cmd));
+		SOCK_put_n_char(self->sock, cmd, strlen(cmd));
 	}
-	SOCK_put_n_char(sock, NULL_STRING, 1);
-	leng = SOCK_flush_output(sock);
+	SOCK_put_n_char(self->sock, NULL_STRING, 1);
+	leng = SOCK_flush_output(self->sock);
 
-	if (SOCK_get_errcode(sock) != 0)
+	if (SOCK_get_errcode(self->sock) != 0)
 	{
 		CC_set_error(self, CONNECTION_COULD_NOT_SEND, "Could not send Query to backend", func);
 		goto cleanup;
 	}
 
 	mylog("send_query: done sending query %dbytes flushed\n", leng);
@@ -2958,40 +2958,40 @@
 		}
 	}
 	res = cmdres;
 	while (!ReadyToReturn)
 	{
 		/* what type of message is coming now ? */
-		id = SOCK_get_id(sock);
+		id = SOCK_get_id(self->sock);
 
-		if ((SOCK_get_errcode(sock) != 0) || (id == EOF))
+		if ((SOCK_get_errcode(self->sock) != 0) || (id == EOF))
 		{
 			CC_set_error(self, CONNECTION_NO_RESPONSE, "No response from the backend", func);
 
 			mylog("send_query: 'id' - %s\n", CC_get_errormsg(self));
 			kill_conn = TRUE;
 			ReadyToReturn = TRUE;
 			break;
 		}
 
 		mylog("send_query: got id = '%c'\n", id);
 
-		response_length = SOCK_get_response_length(sock);
+		response_length = SOCK_get_response_length(self->sock);
 inolog("send_query response_length=%d\n", response_length);
 		switch (id)
 		{
 			case 'A':			/* Asynchronous Messages are ignored */
-				(void) SOCK_get_int(sock, 4);	/* id of notification */
-				SOCK_get_string(sock, msgbuffer, ERROR_MSG_LENGTH);
+				(void) SOCK_get_int(self->sock, 4);	/* id of notification */
+				SOCK_get_string(self->sock, msgbuffer, ERROR_MSG_LENGTH);
 				/* name of the relation the message comes from */
 				break;
 			case 'C':			/* portal query command, no tuples
 								 * returned */
 				/* read in the return message from the backend */
-				SOCK_get_string(sock, cmdbuffer, ERROR_MSG_LENGTH);
-				if (SOCK_get_errcode(sock) != 0)
+				SOCK_get_string(self->sock, cmdbuffer, ERROR_MSG_LENGTH);
+				if (SOCK_get_errcode(self->sock) != 0)
 				{
 					CC_set_error(self, CONNECTION_NO_RESPONSE, "No response from backend while receiving a portal query command", func);
 					mylog("send_query: 'C' - %s\n", CC_get_errormsg(self));
 					ReadyToReturn = TRUE;
 				}
 				else
@@ -3083,14 +3083,14 @@
 					 * empty query, and keep reading out of the pipe until
 					 * an 'I' is received
 					 */
 
 					if (empty_reqs == 0)
 					{
-						SOCK_put_string(sock, "Q ");
-						SOCK_flush_output(sock);
+						SOCK_put_string(self->sock, "Q ");
+						SOCK_flush_output(self->sock);
 						empty_reqs++;
 					}
 				}
 				break;
 			case 'Z':			/* Backend is ready for new query (6.4) */
 				if (empty_reqs == 0)
@@ -3110,14 +3110,14 @@
 
 			case 'I':			/* The server sends an empty query */
 				/* There is a closing '\0' following the 'I', so we eat it */
 				if (PROTOCOL_74(ci) && 0 == response_length)
 					swallow = '\0';
 				else
-					swallow = SOCK_get_char(sock);
-				if ((swallow != '\0') || SOCK_get_errcode(sock) != 0)
+					swallow = SOCK_get_char(self->sock);
+				if ((swallow != '\0') || SOCK_get_errcode(self->sock) != 0)
 				{
 					CC_set_errornumber(self, CONNECTION_BACKEND_CRAZY);
 					QR_set_message(res, "Unexpected protocol character from backend (send_query - I)");
 					QR_set_rstatus(res, PORES_FATAL_ERROR);
 					kill_conn = TRUE;
 					ReadyToReturn = TRUE;
@@ -3143,13 +3143,13 @@
 				aborted = TRUE;
 
 				query_completed = TRUE;
 				break;
 
 			case 'P':			/* get the Portal name */
-				SOCK_get_string(sock, msgbuffer, ERROR_MSG_LENGTH);
+				SOCK_get_string(self->sock, msgbuffer, ERROR_MSG_LENGTH);
 				break;
 			case 'T':			/* Tuple results start here */
 				if (query_completed)
 				{
 					res->next = QR_Constructor();
 					if (!res->next)
@@ -3239,31 +3239,31 @@
 								alsize *= 2;
 								pos = len;
 								continue;
 							}
 						}
 					}
-					SOCK_put_char(sock, 'd'); /* CopyData */
-					SOCK_put_int(sock, 4 + len, 4);
-					SOCK_put_n_char(sock, buf, len);
+					SOCK_put_char(self->sock, 'd'); /* CopyData */
+					SOCK_put_int(self->sock, 4 + len, 4);
+					SOCK_put_n_char(self->sock, buf, len);
 					pos = 0;
 				}
 				if (aborted)
 				{
 mylog("copy fail\n");
-					SOCK_put_char(sock, 'f'); /* CopyFail */
-					SOCK_put_int(sock, 18, 4);
-					SOCK_put_string(sock, "Out of memory");
+					SOCK_put_char(self->sock, 'f'); /* CopyFail */
+					SOCK_put_int(self->sock, 18, 4);
+					SOCK_put_string(self->sock, "Out of memory");
 				}
 				else
 				{
 mylog("copy done\n");
-					SOCK_put_char(sock, 'c'); /* CopyDone */
-					SOCK_put_int(sock, 4, 4);
+					SOCK_put_char(self->sock, 'c'); /* CopyDone */
+					SOCK_put_int(self->sock, 4, 4);
 				}
-				SOCK_flush_output(sock);
+				SOCK_flush_output(self->sock);
 				free(buf);
 				}
 				break;
 			case 'H':			/* Copy out command began successfully */
 				break;
 			case 'c':			/* Copy out command donesuccessfully */
@@ -3313,13 +3313,13 @@
 				mylog("send_query: error - %s\n", CC_get_errormsg(self));
 				ReadyToReturn = TRUE;
 				retres = NULL;
 				break;
 		}
 
-		if (SOCK_get_errcode(sock) != 0)
+		if (SOCK_get_errcode(self->sock) != 0)
 			break;
 		if (CONN_DOWN == self->status)
 			break;
 		/*
 		 * There was no ReadyForQuery response before 6.4.
 		 */
@@ -3328,13 +3328,13 @@
 			if (empty_reqs == 0 && query_completed)
 				break;
 		}
 	}
 
 cleanup:
-	if (SOCK_get_errcode(sock) != 0)
+	if (self->sock && SOCK_get_errcode(self->sock) != 0)
 	{
 		if (0 == CC_get_errornumber(self))
 			CC_set_error(self, CONNECTION_COMMUNICATION_ERROR, "Communication error while sending query", func);
 		kill_conn = TRUE;
 		ReadyToReturn = TRUE;
 	}
