if(!i && (!_kbhit() || !read_tty) ) continue;
if(!i && (!((_kbhit()) || (WAIT_OBJECT_0 == WaitForSingleObject(GetStdHandle(STD_INPUT_HANDLE), 0))) || !read_tty) ) continue;
int MAIN(int argc, char **argv)	{	int off=0;	SSL *con=NULL,*con2=NULL;	int s,k,width,state=0;	char *cbuf=NULL,*sbuf=NULL;	int cbuf_len,cbuf_off;	int sbuf_len,sbuf_off;	fd_set readfds,writefds;	short port=PORT;	int full_log=1;	char *host=SSL_HOST_NAME;	char *cert_file=NULL,*key_file=NULL;	char *CApath=NULL,*CAfile=NULL,*cipher=NULL;	int reconnect=0,badop=0,verify=SSL_VERIFY_NONE,bugs=0;	int crlf=0;	int write_tty,read_tty,write_ssl,read_ssl,tty_on,ssl_pending;	SSL_CTX *ctx=NULL;	int ret=1,in_init=1,i,nbio_test=0;	int prexit = 0;	SSL_METHOD *meth=NULL;	BIO *sbio;#ifdef WINDOWS	struct timeval tv;#endif#if !defined(NO_SSL2) && !defined(NO_SSL3)	meth=SSLv23_client_method();#elif !defined(NO_SSL3)	meth=SSLv3_client_method();#elif !defined(NO_SSL2)	meth=SSLv2_client_method();#endif	apps_startup();	c_Pause=0;	c_quiet=0;	c_ign_eof=0;	c_debug=0;	c_showcerts=0;	if (bio_err == NULL)		bio_err=BIO_new_fp(stderr,BIO_NOCLOSE);	if (	((cbuf=Malloc(BUFSIZZ)) == NULL) ||		((sbuf=Malloc(BUFSIZZ)) == NULL))		{		BIO_printf(bio_err,"out of memory\n");		goto end;		}	verify_depth=0;	verify_error=X509_V_OK;#ifdef FIONBIO	c_nbio=0;#endif	argc--;	argv++;	while (argc >= 1)		{		if	(strcmp(*argv,"-host") == 0)			{			if (--argc < 1) goto bad;			host= *(++argv);			}		else if	(strcmp(*argv,"-port") == 0)			{			if (--argc < 1) goto bad;			port=atoi(*(++argv));			if (port == 0) goto bad;			}		else if (strcmp(*argv,"-connect") == 0)			{			if (--argc < 1) goto bad;			if (!extract_host_port(*(++argv),&host,NULL,&port))				goto bad;			}		else if	(strcmp(*argv,"-verify") == 0)			{			verify=SSL_VERIFY_PEER;			if (--argc < 1) goto bad;			verify_depth=atoi(*(++argv));			BIO_printf(bio_err,"verify depth is %d\n",verify_depth);			}		else if	(strcmp(*argv,"-cert") == 0)			{			if (--argc < 1) goto bad;			cert_file= *(++argv);			}		else if	(strcmp(*argv,"-prexit") == 0)			prexit=1;		else if	(strcmp(*argv,"-crlf") == 0)			crlf=1;		else if	(strcmp(*argv,"-quiet") == 0)			{			c_quiet=1;			c_ign_eof=1;			}		else if	(strcmp(*argv,"-ign_eof") == 0)			c_ign_eof=1;		else if	(strcmp(*argv,"-pause") == 0)			c_Pause=1;		else if	(strcmp(*argv,"-debug") == 0)			c_debug=1;		else if	(strcmp(*argv,"-showcerts") == 0)			c_showcerts=1;		else if	(strcmp(*argv,"-nbio_test") == 0)			nbio_test=1;		else if	(strcmp(*argv,"-state") == 0)			state=1;#ifndef NO_SSL2		else if	(strcmp(*argv,"-ssl2") == 0)			meth=SSLv2_client_method();#endif#ifndef NO_SSL3		else if	(strcmp(*argv,"-ssl3") == 0)			meth=SSLv3_client_method();#endif#ifndef NO_TLS1		else if	(strcmp(*argv,"-tls1") == 0)			meth=TLSv1_client_method();#endif		else if (strcmp(*argv,"-bugs") == 0)			bugs=1;		else if	(strcmp(*argv,"-key") == 0)			{			if (--argc < 1) goto bad;			key_file= *(++argv);			}		else if	(strcmp(*argv,"-reconnect") == 0)			{			reconnect=5;			}		else if	(strcmp(*argv,"-CApath") == 0)			{			if (--argc < 1) goto bad;			CApath= *(++argv);			}		else if	(strcmp(*argv,"-CAfile") == 0)			{			if (--argc < 1) goto bad;			CAfile= *(++argv);			}		else if (strcmp(*argv,"-no_tls1") == 0)			off|=SSL_OP_NO_TLSv1;		else if (strcmp(*argv,"-no_ssl3") == 0)			off|=SSL_OP_NO_SSLv3;		else if (strcmp(*argv,"-no_ssl2") == 0)			off|=SSL_OP_NO_SSLv2;		else if	(strcmp(*argv,"-cipher") == 0)			{			if (--argc < 1) goto bad;			cipher= *(++argv);			}#ifdef FIONBIO		else if (strcmp(*argv,"-nbio") == 0)			{ c_nbio=1; }#endif		else			{			BIO_printf(bio_err,"unknown option %s\n",*argv);			badop=1;			break;			}		argc--;		argv++;		}	if (badop)		{bad:		sc_usage();		goto end;		}	app_RAND_load_file(NULL, bio_err, 0);	if (bio_c_out == NULL)		{		if (c_quiet)			{			bio_c_out=BIO_new(BIO_s_null());			}		else			{			if (bio_c_out == NULL)				bio_c_out=BIO_new_fp(stdout,BIO_NOCLOSE);			}		}	OpenSSL_add_ssl_algorithms();	SSL_load_error_strings();	ctx=SSL_CTX_new(meth);	if (ctx == NULL)		{		ERR_print_errors(bio_err);		goto end;		}	if (bugs)		SSL_CTX_set_options(ctx,SSL_OP_ALL|off);	else		SSL_CTX_set_options(ctx,off);	if (state) SSL_CTX_set_info_callback(ctx,apps_ssl_info_callback);	if (cipher != NULL)		if(!SSL_CTX_set_cipher_list(ctx,cipher)) {		BIO_printf(bio_err,"error setting cipher list\n");		ERR_print_errors(bio_err);		goto end;	}#if 0	else		SSL_CTX_set_cipher_list(ctx,getenv("SSL_CIPHER"));#endif	SSL_CTX_set_verify(ctx,verify,verify_callback);	if (!set_cert_stuff(ctx,cert_file,key_file))		goto end;	if ((!SSL_CTX_load_verify_locations(ctx,CAfile,CApath)) ||		(!SSL_CTX_set_default_verify_paths(ctx)))		{		/* BIO_printf(bio_err,"error setting default verify locations\n"); */		ERR_print_errors(bio_err);		/* goto end; */		}	con=SSL_new(ctx);/*	SSL_set_cipher_list(con,"RC4-MD5"); */re_start:	if (init_client(&s,host,port) == 0)		{		BIO_printf(bio_err,"connect:errno=%d\n",get_last_socket_error());		SHUTDOWN(s);		goto end;		}	BIO_printf(bio_c_out,"CONNECTED(%08X)\n",s);#ifdef FIONBIO	if (c_nbio)		{		unsigned long l=1;		BIO_printf(bio_c_out,"turning on non blocking io\n");		if (BIO_socket_ioctl(s,FIONBIO,&l) < 0)			{			ERR_print_errors(bio_err);			goto end;			}		}#endif                                              	if (c_Pause & 0x01) con->debug=1;	sbio=BIO_new_socket(s,BIO_NOCLOSE);	if (nbio_test)		{		BIO *test;		test=BIO_new(BIO_f_nbio_test());		sbio=BIO_push(test,sbio);		}	if (c_debug)		{		con->debug=1;		BIO_set_callback(sbio,bio_dump_cb);		BIO_set_callback_arg(sbio,bio_c_out);		}	SSL_set_bio(con,sbio,sbio);	SSL_set_connect_state(con);	/* ok, lets connect */	width=SSL_get_fd(con)+1;	read_tty=1;	write_tty=0;	tty_on=0;	read_ssl=1;	write_ssl=1;		cbuf_len=0;	cbuf_off=0;	sbuf_len=0;	sbuf_off=0;	for (;;)		{		FD_ZERO(&readfds);		FD_ZERO(&writefds);		if (SSL_in_init(con) && !SSL_total_renegotiations(con))			{			in_init=1;			tty_on=0;			}		else			{			tty_on=1;			if (in_init)				{				in_init=0;				print_stuff(bio_c_out,con,full_log);				if (full_log > 0) full_log--;				if (reconnect)					{					reconnect--;					BIO_printf(bio_c_out,"drop connection and then reconnect\n");					SSL_shutdown(con);					SSL_set_connect_state(con);					SHUTDOWN(SSL_get_fd(con));					goto re_start;					}				}			}		ssl_pending = read_ssl && SSL_pending(con);		if (!ssl_pending)			{#ifndef WINDOWS			if (tty_on)				{				if (read_tty)  FD_SET(fileno(stdin),&readfds);				if (write_tty) FD_SET(fileno(stdout),&writefds);				}			if (read_ssl)				FD_SET(SSL_get_fd(con),&readfds);			if (write_ssl)				FD_SET(SSL_get_fd(con),&writefds);#else			if(!tty_on || !write_tty) {				if (read_ssl)					FD_SET(SSL_get_fd(con),&readfds);				if (write_ssl)					FD_SET(SSL_get_fd(con),&writefds);			}#endif/*			printf("mode tty(%d %d%d) ssl(%d%d)\n",				tty_on,read_tty,write_tty,read_ssl,write_ssl);*/			/* Note: under VMS with SOCKETSHR the second parameter			 * is currently of type (int *) whereas under other			 * systems it is (void *) if you don't have a cast it			 * will choke the compiler: if you do have a cast then			 * you can either go for (int *) or (void *).			 */#ifdef WINDOWS			/* Under Windows we make the assumption that we can			 * always write to the tty: therefore if we need to			 * write to the tty we just fall through. Otherwise			 * we timeout the select every second and see if there			 * are any keypresses. Note: this is a hack, in a proper			 * Windows application we wouldn't do this.			 */			i=0;			if(!write_tty) {				if(read_tty) {					tv.tv_sec = 1;					tv.tv_usec = 0;					i=select(width,(void *)&readfds,(void *)&writefds,						 NULL,&tv);					if(!i && (!_kbhit() || !read_tty) ) continue;				} else 	i=select(width,(void *)&readfds,(void *)&writefds,					 NULL,NULL);			}#else			i=select(width,(void *)&readfds,(void *)&writefds,				 NULL,NULL);#endif			if ( i < 0)				{				BIO_printf(bio_err,"bad select %d\n",				get_last_socket_error());				goto shut;				/* goto end; */				}			}		if (!ssl_pending && FD_ISSET(SSL_get_fd(con),&writefds))			{			k=SSL_write(con,&(cbuf[cbuf_off]),				(unsigned int)cbuf_len);			switch (SSL_get_error(con,k))				{			case SSL_ERROR_NONE:				cbuf_off+=k;				cbuf_len-=k;				if (k <= 0) goto end;				/* we have done a  write(con,NULL,0); */				if (cbuf_len <= 0)					{					read_tty=1;					write_ssl=0;					}				else /* if (cbuf_len > 0) */					{					read_tty=0;					write_ssl=1;					}				break;			case SSL_ERROR_WANT_WRITE:				BIO_printf(bio_c_out,"write W BLOCK\n");				write_ssl=1;				read_tty=0;				break;			case SSL_ERROR_WANT_READ:				BIO_printf(bio_c_out,"write R BLOCK\n");				write_tty=0;				read_ssl=1;				write_ssl=0;				break;			case SSL_ERROR_WANT_X509_LOOKUP:				BIO_printf(bio_c_out,"write X BLOCK\n");				break;			case SSL_ERROR_ZERO_RETURN:				if (cbuf_len != 0)					{					BIO_printf(bio_c_out,"shutdown\n");					goto shut;					}				else					{					read_tty=1;					write_ssl=0;					break;					}							case SSL_ERROR_SYSCALL:				if ((k != 0) || (cbuf_len != 0))					{					BIO_printf(bio_err,"write:errno=%d\n",						get_last_socket_error());					goto shut;					}				else					{					read_tty=1;					write_ssl=0;					}				break;			case SSL_ERROR_SSL:				ERR_print_errors(bio_err);				goto shut;				}			}#ifdef WINDOWS		/* Assume Windows can always write */		else if (!ssl_pending && write_tty)#else		else if (!ssl_pending && FD_ISSET(fileno(stdout),&writefds))#endif			{#ifdef CHARSET_EBCDIC			ascii2ebcdic(&(sbuf[sbuf_off]),&(sbuf[sbuf_off]),sbuf_len);#endif			i=write(fileno(stdout),&(sbuf[sbuf_off]),sbuf_len);			if (i <= 0)				{				BIO_printf(bio_c_out,"DONE\n");				goto shut;				/* goto end; */				}			sbuf_len-=i;;			sbuf_off+=i;			if (sbuf_len <= 0)				{				read_ssl=1;				write_tty=0;				}			}		else if (ssl_pending || FD_ISSET(SSL_get_fd(con),&readfds))			{#ifdef RENEG{ static int iiii; if (++iiii == 52) { SSL_renegotiate(con); iiii=0; } }#endif#if 1			k=SSL_read(con,sbuf,1024 /* BUFSIZZ */ );#else/* Demo for pending and peek :-) */			k=SSL_read(con,sbuf,16);{ char zbuf[10240]; printf("read=%d pending=%d peek=%d\n",k,SSL_pending(con),SSL_peek(con,zbuf,10240));}#endif			switch (SSL_get_error(con,k))				{			case SSL_ERROR_NONE:				if (k <= 0)					goto end;				sbuf_off=0;				sbuf_len=k;				read_ssl=0;				write_tty=1;				break;			case SSL_ERROR_WANT_WRITE:				BIO_printf(bio_c_out,"read W BLOCK\n");				write_ssl=1;				read_tty=0;				break;			case SSL_ERROR_WANT_READ:				BIO_printf(bio_c_out,"read R BLOCK\n");				write_tty=0;				read_ssl=1;				if ((read_tty == 0) && (write_ssl == 0))					write_ssl=1;				break;			case SSL_ERROR_WANT_X509_LOOKUP:				BIO_printf(bio_c_out,"read X BLOCK\n");				break;			case SSL_ERROR_SYSCALL:				BIO_printf(bio_err,"read:errno=%d\n",get_last_socket_error());				goto shut;			case SSL_ERROR_ZERO_RETURN:				BIO_printf(bio_c_out,"closed\n");				goto shut;			case SSL_ERROR_SSL:				ERR_print_errors(bio_err);				goto shut;				/* break; */				}			}#ifdef WINDOWS		else if (_kbhit())#else		else if (FD_ISSET(fileno(stdin),&readfds))#endif			{			if (crlf)				{				int j, lf_num;				i=read(fileno(stdin),cbuf,BUFSIZZ/2);				lf_num = 0;				/* both loops are skipped when i <= 0 */				for (j = 0; j < i; j++)					if (cbuf[j] == '\n')						lf_num++;				for (j = i-1; j >= 0; j--)					{					cbuf[j+lf_num] = cbuf[j];					if (cbuf[j] == '\n')						{						lf_num--;						i++;						cbuf[j+lf_num] = '\r';						}					}				assert(lf_num == 0);				}			else				i=read(fileno(stdin),cbuf,BUFSIZZ);			if ((!c_ign_eof) && ((i <= 0) || (cbuf[0] == 'Q')))				{				BIO_printf(bio_err,"DONE\n");				goto shut;				}			if ((!c_ign_eof) && (cbuf[0] == 'R'))				{				BIO_printf(bio_err,"RENEGOTIATING\n");				SSL_renegotiate(con);				cbuf_len=0;				}			else				{				cbuf_len=i;				cbuf_off=0;#ifdef CHARSET_EBCDIC				ebcdic2ascii(cbuf, cbuf, i);#endif				}			write_ssl=1;			read_tty=0;			}		}shut:	SSL_shutdown(con);	SHUTDOWN(SSL_get_fd(con));	ret=0;end:	if(prexit) print_stuff(bio_c_out,con,1);	if (con != NULL) SSL_free(con);	if (con2 != NULL) SSL_free(con2);	if (ctx != NULL) SSL_CTX_free(ctx);	if (cbuf != NULL) { memset(cbuf,0,BUFSIZZ); Free(cbuf); }	if (sbuf != NULL) { memset(sbuf,0,BUFSIZZ); Free(sbuf); }	if (bio_c_out != NULL)		{		BIO_free(bio_c_out);		bio_c_out=NULL;		}	EXIT(ret);	}