AcceptConnReq(ptcplstn_t *const pLstn, int *const newSock, prop_t **peerName, prop_t **peerIP)
{
	int sockflags;
	struct sockaddr_storage addr;
	socklen_t addrlen = sizeof(addr);
	int iNewSock = -1;

	DEFiRet;

	*peerName = NULL; /* ensure we know if we don't have one! */
	iNewSock = accept(pLstn->sock, (struct sockaddr*) &addr, &addrlen);
	if(iNewSock < 0) {
		if(CHK_EAGAIN_EWOULDBLOCK || errno == EMFILE)
			ABORT_FINALIZE(RS_RET_NO_MORE_DATA);
		LogError(errno, RS_RET_ACCEPT_ERR, "error accepting connection "
			    "on listen socket %d", pLstn->sock);
		ABORT_FINALIZE(RS_RET_ACCEPT_ERR);
	}
	if(addrlen == 0) {
		LogError(errno, RS_RET_ACCEPT_ERR, "AcceptConnReq could not obtain "
			    "remote peer identification on listen socket %d", pLstn->sock);
	}

	if(pLstn->pSrv->bKeepAlive)
		EnableKeepAlive(pLstn, iNewSock);/* we ignore errors, best to do! */

	CHKiRet(getPeerNames(peerName, peerIP, (struct sockaddr *) &addr, pLstn->pSrv->bUnixSocket));

	/* set the new socket to non-blocking IO */
	if((sockflags = fcntl(iNewSock, F_GETFL)) != -1) {
		sockflags |= O_NONBLOCK;
		/* SETFL could fail too, so get it caught by the subsequent
		 * error check.
		 */
		sockflags = fcntl(iNewSock, F_SETFL, sockflags);
	}

	if(sockflags == -1) {
		LogError(errno, RS_RET_IO_ERROR, "error setting fcntl(O_NONBLOCK) on "
			"tcp socket %d", iNewSock);
		prop.Destruct(peerName);
		prop.Destruct(peerIP);
		ABORT_FINALIZE(RS_RET_IO_ERROR);
	}
	if(pLstn->pSrv->bEmitMsgOnOpen) {
		LogMsg(0, RS_RET_NO_ERRCODE, LOG_INFO,
			"imptcp: connection established with host: %s",
			propGetSzStr(*peerName));
	}

	STATSCOUNTER_INC(pLstn->ctrSessOpen, pLstn->mutCtrSessOpen);
	*newSock = iNewSock;

finalize_it:
	DBGPRINTF("iRet: %d\n", iRet);
	if(iRet != RS_RET_OK) {
		if(iRet != RS_RET_NO_MORE_DATA && pLstn->pSrv->bEmitMsgOnOpen) {
			LogError(0, NO_ERRCODE, "imptcp: connection could not be "
				"established with host: %s",
				*peerName == NULL ? "(could not query)"
					: (const char*)propGetSzStr(*peerName));
		}
		STATSCOUNTER_INC(pLstn->ctrSessOpenErr, pLstn->mutCtrSessOpenErr);
		/* the close may be redundant, but that doesn't hurt... */
		if(iNewSock != -1)
			close(iNewSock);
	}

	RETiRet;
}
AcceptConnReq(ptcplstn_t *const pLstn, int *const newSock, prop_t **peerName, prop_t **peerIP)
{
	int sockflags;
	struct sockaddr_storage addr;
	socklen_t addrlen = sizeof(addr);
	int iNewSock = -1;

	DEFiRet;

	*peerName = NULL;  
	iNewSock = accept(pLstn->sock, (struct sockaddr*) &addr, &addrlen);
	if(iNewSock < 0) {
		if(CHK_EAGAIN_EWOULDBLOCK || errno == EMFILE)
			ABORT_FINALIZE(RS_RET_NO_MORE_DATA);
		LogError(errno, RS_RET_ACCEPT_ERR, "error accepting connection "
			    "on listen socket %d", pLstn->sock);
		ABORT_FINALIZE(RS_RET_ACCEPT_ERR);
	}
	if(addrlen == 0) {
		LogError(errno, RS_RET_ACCEPT_ERR, "AcceptConnReq could not obtain "
			    "remote peer identification on listen socket %d", pLstn->sock);
	}

	if(pLstn->pSrv->bKeepAlive)
		EnableKeepAlive(pLstn, iNewSock); 

	CHKiRet(getPeerNames(peerName, peerIP, (struct sockaddr *) &addr, pLstn->pSrv->bUnixSocket));

	 
	if((sockflags = fcntl(iNewSock, F_GETFL)) != -1) {
		sockflags |= O_NONBLOCK;
		 
		sockflags = fcntl(iNewSock, F_SETFL, sockflags);
	}

	if(sockflags == -1) {
		LogError(errno, RS_RET_IO_ERROR, "error setting fcntl(O_NONBLOCK) on "
			"tcp socket %d", iNewSock);
		prop.Destruct(peerName);
		prop.Destruct(peerIP);
		ABORT_FINALIZE(RS_RET_IO_ERROR);
	}
	if(pLstn->pSrv->bEmitMsgOnOpen) {
		LogMsg(0, RS_RET_NO_ERRCODE, LOG_INFO,
			"imptcp: connection established with host: %s",
			propGetSzStr(*peerName));
	}

	STATSCOUNTER_INC(pLstn->ctrSessOpen, pLstn->mutCtrSessOpen);
	*newSock = iNewSock;

finalize_it:
	DBGPRINTF("iRet: %d\n", iRet);
	if(iRet != RS_RET_OK) {
		if(iRet != RS_RET_NO_MORE_DATA && pLstn->pSrv->bEmitMsgOnOpen) {
			LogError(0, NO_ERRCODE, "imptcp: connection could not be "
				"established with host: %s",
				*peerName == NULL ? "(could not query)"
					: (const char*)propGetSzStr(*peerName));
		}
		STATSCOUNTER_INC(pLstn->ctrSessOpenErr, pLstn->mutCtrSessOpenErr);
		 
		if(iNewSock != -1)
			close(iNewSock);
	}

	RETiRet;
}