/*
 * See #new.
 */
static VALUE
pgconn_s_connect(argc, argv, pgconn)
  int argc;
  VALUE *argv;
  VALUE pgconn;
{
  VALUE arg[7];
  char *pghost, *pgopt, *pgtty, *pgdbname, *pglogin, *pgpwd;
  int pgport;
  char port_buffer[20];
  PGconn *conn;

  pghost=pgopt=pgtty=pgdbname=pglogin=pgpwd=NULL;
  pgport = -1;
  rb_scan_args(argc,argv,"07", &arg[0], &arg[1], &arg[2], &arg[3], &arg[4],
                &arg[5], &arg[6]);
  if (!NIL_P(arg[0])) {
  Check_Type(arg[0], T_STRING);
  pghost = STR2CSTR(arg[0]);
  }
  if (!NIL_P(arg[1])) {
  pgport = NUM2INT(arg[1]);
  }
  if (!NIL_P(arg[2])) {
  Check_Type(arg[2], T_STRING);
  pgopt = STR2CSTR(arg[2]);
  }
  if (!NIL_P(arg[3])) {
  Check_Type(arg[3], T_STRING);
  pgtty = STR2CSTR(arg[3]);
  }
  if (!NIL_P(arg[4])) {
  Check_Type(arg[4], T_STRING);
  pgdbname = STR2CSTR(arg[4]);
  }
  if (!NIL_P(arg[5])) {
    Check_Type(arg[5], T_STRING);
    pglogin = STR2CSTR(arg[5]);
  }
  if (!NIL_P(arg[6])) {
    Check_Type(arg[6], T_STRING);
    pgpwd = STR2CSTR(arg[6]);
  }
  if (pgport!=-1) {
  sprintf(port_buffer, "%d", pgport);
    conn = PQsetdbLogin(pghost, port_buffer, pgopt, pgtty, pgdbname,
              pglogin, pgpwd);
  }
  else {
    conn = PQsetdbLogin(pghost, NULL, pgopt, pgtty, pgdbname,
              pglogin, pgpwd);
  }

  if (PQstatus(conn) == CONNECTION_BAD) {
  rb_raise(rb_ePGError, PQerrorMessage(conn));
  }

  return Data_Wrap_Struct(pgconn, 0, free_pgconn, conn);
}