. Fix some header files depedecies.
 #include <errno.h>
 #endif /* WIN32 */
 
+#include <libpq-fe.h>
 #include "environ.h"
 #include "socket.h"
 #include "statement.h"
        CC_on_abort(self, abort_opt);
    if (res)
    {
-       QR_set_rstatus(res, PGRES_FATAL_ERROR);
+       QR_set_rstatus(res, PORES_FATAL_ERROR);
        QR_set_message(res, msgbuf);
        QR_set_aborted(res, TRUE);
    }
    if (res)
    {
        if (QR_command_successful(res))
-           QR_set_rstatus(res, PGRES_NONFATAL_ERROR);
+           QR_set_rstatus(res, PORES_NONFATAL_ERROR);
        QR_set_notice(res, msgbuf);  /* will dup this string */
    }
 
 
        res = CC_send_query(self, " ", NULL, 0, NULL);
        if (res == NULL ||
-           (QR_get_rstatus(res) != PGRES_EMPTY_QUERY &&
+           (QR_get_rstatus(res) != PORES_EMPTY_QUERY &&
             QR_command_nonfatal(res)))
        {
            CC_set_error(self, CONNECTION_NO_SUCH_DATABASE, "The database does not exist on the server\nor user authentication failed.", func);
                    }
 
                    if (QR_command_successful(res))
-                       QR_set_rstatus(res, PGRES_COMMAND_OK);
+                       QR_set_rstatus(res, PORES_COMMAND_OK);
                    QR_set_command(res, cmdbuffer);
                    query_completed = TRUE;
                    mylog("send_query: returning res = %x\n", res);
                {
                    CC_set_errornumber(self, CONNECTION_BACKEND_CRAZY);
                    QR_set_message(res, "Unexpected protocol character from backend (send_query - I)");
-                   QR_set_rstatus(res, PGRES_FATAL_ERROR);
+                   QR_set_rstatus(res, PORES_FATAL_ERROR);
                    ReadyToReturn = TRUE;
                    retres = cmdres;
                    break;
                else
                {
                    /* We return the empty query */
-                   QR_set_rstatus(res, PGRES_EMPTY_QUERY);
+                   QR_set_rstatus(res, PORES_EMPTY_QUERY);
                }
                if (empty_reqs > 0)
                {
                    {
                        CC_set_error(self, CONNECTION_COULD_NOT_RECEIVE, QR_get_message(res), func);
                        ReadyToReturn = TRUE;
-                       if (PGRES_FATAL_ERROR == QR_get_rstatus(res))
+                       if (PORES_FATAL_ERROR == QR_get_rstatus(res))
                            retres = cmdres;
                        else
                            retres = NULL;
                    res->next = QR_Constructor();
                    res = res->next;
                }
-               QR_set_rstatus(res, PGRES_COPY_IN);
+               QR_set_rstatus(res, PORES_COPY_IN);
                ReadyToReturn = TRUE;
                retres = cmdres;
                break;
                    res->next = QR_Constructor();
                    res = res->next;
                }
-               QR_set_rstatus(res, PGRES_COPY_OUT);
+               QR_set_rstatus(res, PORES_COPY_OUT);
                ReadyToReturn = TRUE;
                retres = cmdres;
                break;
 
 #undef memset
 */
 #include   "misc.h"
+#ifdef WIN32
 #include   <malloc.h>
+#endif /* WIN32 */
 #ifdef _DEBUG
 #include   <stdlib.h>
 #define    _CRTDBG_MAP_ALLOC
 
  *
  * Comments:       See "notice.txt" for copyright and license information.
  *
- * $Id: psqlodbc.h,v 1.104 2006/08/24 15:03:56 hinoue Exp $
+ * $Id: psqlodbc.h,v 1.105 2006/08/26 22:54:16 hinoue Exp $
  *
  */
 
 #include "iodbc.h"
 #include "isql.h"
 #include "isqlext.h"
-#endif
+#endif /* WIN32 */
 
 #if defined(WIN32)
 #include <odbcinst.h>
 #include "gpps.h"
 #endif
 
-/* #include <libpq-fe.h> */
-
 #ifdef __cplusplus
 extern "C" {
 #endif
-#ifndef WIN32
+
 #define Int4 int
 #define UInt4 unsigned int
 #define Int2 short
 #define UInt2 unsigned short
 
+#ifndef WIN32
 #if !defined(WITH_UNIXODBC) && !defined(WITH_IODBC)
 typedef float SFLOAT;
 typedef double SDOUBLE;
-#endif
+#endif /* WITH_UNIXODBC */
 
 #ifndef CALLBACK
 #define CALLBACK
-#endif
+#endif /* CALLBACK */
+#endif /* WIN32 */
 
-#else
-#define Int4 int
-#define UInt4 unsigned int
-#define Int2 short
-#define UInt2 unsigned short
-#endif
+#ifndef LIBPQ_FE_H
 typedef    UInt4   Oid;
+#endif /* LIBPQ_FE_H */
 
 #ifndef WIN32
 #define stricmp strcasecmp
 #define strnicmp _strnicmp
 #define stricmp _stricmp
 #define vsnprintf _vsnprintf
-#endif
+#endif /* WIN32 */
 
 #ifndef    SQL_ATTR_APP_ROW_DESC
 #define    SQL_ATTR_APP_ROW_DESC   10010
 
 
    if (rv != NULL)
    {
-       rv->rstatus = PGRES_EMPTY_QUERY;
+       rv->rstatus = PORES_EMPTY_QUERY;
        rv->pstatus = 0;
 
        /* construct the column info */
        {
            if (!cursor)
            {
-               QR_set_rstatus(self, PGRES_INTERNAL_ERROR);
+               QR_set_rstatus(self, PORES_INTERNAL_ERROR);
                QR_set_message(self, "Internal Error -- no cursor for fetch");
                return FALSE;
            }
         */
        if (CI_read_fields(self->fields, QR_get_conn(self)))
        {
-           QR_set_rstatus(self, PGRES_FIELDS_OK);
+           QR_set_rstatus(self, PORES_FIELDS_OK);
            self->num_fields = CI_get_num_fields(self->fields);
            if (QR_haskeyset(self))
                self->num_fields -= self->num_key_fields;
        }
        else
        {
-           QR_set_rstatus(self, PGRES_BAD_RESPONSE);
+           QR_set_rstatus(self, PORES_BAD_RESPONSE);
            QR_set_message(self, "Error reading field information");
            return FALSE;
        }
        {
            if (self->keyset = (KeySet *) calloc(sizeof(KeySet), tuple_size), !self->keyset)
            {
-               QR_set_rstatus(self, PGRES_FATAL_ERROR);
+               QR_set_rstatus(self, PORES_FATAL_ERROR);
                QR_set_message(self, "Could not get memory for tuple cache.");
                return FALSE;
            }
 
        if (!CI_read_fields(NULL, QR_get_conn(self)))
        {
-           QR_set_rstatus(self, PGRES_BAD_RESPONSE);
+           QR_set_rstatus(self, PORES_BAD_RESPONSE);
            QR_set_message(self, "Error reading field information");
            return FALSE;
        }
 
            if (!CC_commit(conn))
            {
-               QR_set_rstatus(self, PGRES_FATAL_ERROR);
+               QR_set_rstatus(self, PORES_FATAL_ERROR);
                QR_set_message(self, "Error ending transaction.");
                ret = FALSE;
            }
 
    if (!QR_read_a_tuple_from_db(self, (char) binary))
    {
-       QR_set_rstatus(self, PGRES_BAD_RESPONSE);
+       QR_set_rstatus(self, PORES_BAD_RESPONSE);
        QR_set_message(self, "Error reading the tuple");
        return FALSE;
    }
            res = CC_send_query(conn, fetch, &qi, 0, stmt);
            if (!QR_command_maybe_successful(res))
            {
-               QR_set_rstatus(self, PGRES_FATAL_ERROR);
+               QR_set_rstatus(self, PORES_FATAL_ERROR);
                QR_set_message(self, "Error fetching next group.");
                return FALSE;
            }
                mylog("%s: Unexpected result from backend: id = '%c' (%d)\n", func, id, id);
                qlog("%s: Unexpected result from backend: id = '%c' (%d)\n", func, id, id);
                QR_set_message(self, "Unexpected result from backend. It probably crashed");
-               QR_set_rstatus(self, PGRES_FATAL_ERROR);
+               QR_set_rstatus(self, PORES_FATAL_ERROR);
                CC_on_abort(conn, CONN_DEAD);
                ret = FALSE;
                rcvend = TRUE;
 
 #include "columninfo.h"
 #include "tuple.h"
 
-#include   <libpq-fe.h>
-
 #ifdef __cplusplus
 extern "C" {
 #endif
 
-typedef ExecStatusType QueryResultCode;
-#define    PGRES_FIELDS_OK     100
-#define    PGRES_INTERNAL_ERROR    (PGRES_FIELDS_OK + 1)
+typedef
+enum   QueryResultCode_
+{
+   PORES_EMPTY_QUERY = 0,
+   PORES_COMMAND_OK,   /* a query command that doesn't return
+                * anything was executed properly by the backend */
+   PORES_TUPLES_OK,    /* a query command that returns tuples
+                * was executed properly by the backend, PGresult
+                * contains the resulttuples */
+   PORES_COPY_OUT,
+   PORES_COPY_IN,
+   PORES_BAD_RESPONSE, /* an unexpected response was recv'd from
+                * the backend */
+   PORES_NONFATAL_ERROR,
+   PORES_FATAL_ERROR,
+   PORES_FIELDS_OK = 100,  /* field information from a query was
+                * successful */
+   /* PORES_END_TUPLES, */
+   PORES_INTERNAL_ERROR
+} QueryResultCode;
 
 enum
 {
 #define QR_set_field_info_v(self, field_num, name, adtid, adtsize)  (CI_set_field_info(self->fields, field_num, name, adtid, adtsize, -1, 0, 0))
 
 /* status macros */
-#define QR_command_successful(self)    (self && !(self->rstatus == PGRES_BAD_RESPONSE || self->rstatus == PGRES_NONFATAL_ERROR || self->rstatus == PGRES_FATAL_ERROR))
-#define QR_command_maybe_successful(self) (self && !(self->rstatus == PGRES_BAD_RESPONSE || self->rstatus == PGRES_FATAL_ERROR))
-#define QR_command_nonfatal(self)  ( self->rstatus == PGRES_NONFATAL_ERROR)
+#define QR_command_successful(self)    (self && !(self->rstatus == PORES_BAD_RESPONSE || self->rstatus == PORES_NONFATAL_ERROR || self->rstatus == PORES_FATAL_ERROR))
+#define QR_command_maybe_successful(self) (self && !(self->rstatus == PORES_BAD_RESPONSE || self->rstatus == PORES_FATAL_ERROR))
+#define QR_command_nonfatal(self)  ( self->rstatus == PORES_NONFATAL_ERROR)
 #define QR_set_conn(self, conn_)           ( self->conn = conn_ )
 #define QR_set_rstatus(self, condition)        ( self->rstatus = condition )
 #define QR_set_sqlstatus(self, status)     strcpy(self->sqlstatus, status)
 do { \
    if (t = (tp *) malloc(s), NULL == t) \
    { \
-       QR_set_rstatus(a, PGRES_FATAL_ERROR); \
+       QR_set_rstatus(a, PORES_FATAL_ERROR); \
        QR_set_message(a, m); \
        return r; \
    } \
 do { \
    if (t = (tp *) realloc(t, s), NULL == t) \
    { \
-       QR_set_rstatus(a, PGRES_FATAL_ERROR); \
+       QR_set_rstatus(a, PORES_FATAL_ERROR); \
        QR_set_message(a, m); \
        return r; \
    } \
 
                        res->num_fields * sizeof(TupleField) * tuple_size);
                    if (!res->backend_tuples)
                    {
-                       SC_set_error(stmt, QR_set_rstatus(res, PGRES_FATAL_ERROR), "Out of memory while reading tuples.", func);
+                       SC_set_error(stmt, QR_set_rstatus(res, PORES_FATAL_ERROR), "Out of memory while reading tuples.", func);
                        QR_Destructor(qres);
                        return SQL_ERROR;
                    }
 
        do {
            FD_ZERO(&fds);
            FD_SET(self->socket, &fds);
-           ret = select(1, NULL, &fds, NULL, timeout > 0 ? &tm : NULL);
+           ret = select(self->socket + 1, NULL, &fds, NULL, timeout > 0 ? &tm : NULL);
            if (0 < ret)
                break;
            else if (0 == ret)
            tm.tv_sec = retry_count;
            tm.tv_usec = 0;
        }
-       ret = select(1, output ? NULL : &fds, output ? &fds : NULL, NULL, sock->ssl ? &tm : NULL);
+       ret = select(sock->socket + 1, output ? NULL : &fds, output ? &fds : NULL, NULL, sock->ssl ? &tm : NULL);
    } while (ret < 0 && EINTR == SOCK_ERRNO);
    if (0 == ret && retry_count > MAX_RETRY_COUNT)
    {
 
        if (!SC_is_pre_executable(self))
        {
            SC_set_Result(self, QR_Constructor());
-           QR_set_rstatus(SC_get_Result(self), PGRES_TUPLES_OK);
+           QR_set_rstatus(SC_get_Result(self), PORES_TUPLES_OK);
            self->inaccurate_result = TRUE;
            self->status = STMT_PREMATURE;
            num_fields = 0;
                    Int2    dummy1, dummy2;
                    int cidx;
 
-                   QR_set_rstatus(res, PGRES_FIELDS_OK);
+                   QR_set_rstatus(res, PORES_FIELDS_OK);
                    res->num_fields = CI_get_num_fields(res->fields);
                    if (QR_haskeyset(res))
                        res->num_fields -= res->num_key_fields;
                }
                else
                {
-                   QR_set_rstatus(res, PGRES_BAD_RESPONSE);
+                   QR_set_rstatus(res, PORES_BAD_RESPONSE);
                    QR_set_message(res, "Error reading field information");
                    rcvend = TRUE;
                }
 
 
 #define POSTGRESDRIVERVERSION      "08.02.0007"
 #define POSTGRES_RESOURCE_VERSION  "08.02.0007\0"
-#define PG_DRVFILE_VERSION     8,2,0,07 
+#define PG_DRVFILE_VERSION     8,2,0,07  
 
 #endif
 
 #include "psqlodbc.h"
 #include <stdio.h>
 #include <string.h>
+#ifdef WIN32
 #include <malloc.h>
+#endif /* WIN32 */
 
 #define    byte3check  0xfffff800
 #define    byte2_base  0x80c0