Index: libfdcore/fdcore-internal.h
===================================================================
--- libfdcore/fdcore-internal.h	(revision 1207)
+++ libfdcore/fdcore-internal.h	(revision 1238)
@@ -297,5 +297,5 @@
 
 /* Peer out */
-int fd_out_send(struct msg ** msg, struct cnxctx * cnx, struct fd_peer * peer);
+int fd_out_send(struct msg ** msg, struct cnxctx * cnx, struct fd_peer * peer, int update_reqin_cnt);
 int fd_out_start(struct fd_peer * peer);
 int fd_out_stop(struct fd_peer * peer);
Index: libfdcore/p_ce.c
===================================================================
--- libfdcore/p_ce.c	(revision 1230)
+++ libfdcore/p_ce.c	(revision 1238)
@@ -635,5 +635,5 @@
 	CHECK_FCT_DO( fd_msg_new_answer_from_req ( fd_g_config->cnf_dict, cer, MSGFL_ANSW_ERROR ), goto destroy );
 	CHECK_FCT_DO( fd_msg_rescode_set(*cer, error->pei_errcode, error->pei_message, error->pei_avp, 1 ), goto destroy );
-	CHECK_FCT_DO( fd_out_send(cer, *recv_cnx, NULL), goto destroy );
+	CHECK_FCT_DO( fd_out_send(cer, *recv_cnx, NULL, 0), goto destroy );
 	
 	if (error->pei_avp_free) {
@@ -659,5 +659,5 @@
 	/* Send CER on the new connection */
 	CHECK_FCT( create_CER(peer, initiator, &cer) );
-	CHECK_FCT( fd_out_send(&cer, initiator, peer) );
+	CHECK_FCT( fd_out_send(&cer, initiator, peer, 0) );
 	
 	/* Are we doing an election ? */
@@ -714,5 +714,5 @@
 
 		/* msg now contains an answer message to send back */
-		CHECK_FCT_DO( fd_out_send(msg, NULL, peer), /* In case of error the message has already been dumped */ );
+		CHECK_FCT_DO( fd_out_send(msg, NULL, peer, 0), /* In case of error the message has already been dumped */ );
 	}
 	
@@ -943,9 +943,4 @@
 	}
 	
-	/* Update the counter to match with the answer being sent */
-	CHECK_POSIX( pthread_mutex_lock(&peer->p_state_mtx) );
-	peer->p_reqin_count++;
-	CHECK_POSIX( pthread_mutex_unlock(&peer->p_state_mtx) );
-
 	/* Reply a CEA */
 	CHECK_FCT( fd_msg_new_answer_from_req ( fd_g_config->cnf_dict, &msg, 0 ) );
@@ -956,5 +951,5 @@
 	fd_hook_call(HOOK_PEER_CONNECT_SUCCESS, msg, peer, NULL, NULL);
 	
-	CHECK_FCT( fd_out_send(&msg, peer->p_cnxctx, peer ) );
+	CHECK_FCT( fd_out_send(&msg, peer->p_cnxctx, peer, 0 ) );
 	
 	/* Handshake if needed */
Index: libfdcore/p_dp.c
===================================================================
--- libfdcore/p_dp.c	(revision 1186)
+++ libfdcore/p_dp.c	(revision 1238)
@@ -113,10 +113,10 @@
 		CHECK_FCT( fd_peer_get_load_pending(&peer->p_hdr, &to_receive, &to_send) );
 		
-		if ((to_receive == 0) && (to_send == 1 /* only the DPA */)) {
+		if ((to_receive == 0) && (to_send == 0)) {
 			/* No pending exchange, move to CLOSING directly */
 			CHECK_FCT( fd_psm_change_state(peer, STATE_CLOSING) );
 		
 			/* Now send the DPA */
-			CHECK_FCT( fd_out_send( msg, NULL, peer) );
+			CHECK_FCT( fd_out_send( msg, NULL, peer, 0) );
 			
 			/* and move to CLOSED */
@@ -132,5 +132,5 @@
 			
 			/* Now send the DPA */
-			CHECK_FCT( fd_out_send( msg, NULL, peer) );
+			CHECK_FCT( fd_out_send( msg, NULL, peer, 0) );
 		}
 	} else {
@@ -202,5 +202,5 @@
 	
 	/* Now send the DPR message */
-	CHECK_FCT_DO( fd_out_send(&msg, NULL, peer), /* ignore since we are on timeout anyway */ );
+	CHECK_FCT_DO( fd_out_send(&msg, NULL, peer, 0), /* ignore since we are on timeout anyway */ );
 	
 	return 0;
Index: libfdcore/p_dw.c
===================================================================
--- libfdcore/p_dw.c	(revision 1186)
+++ libfdcore/p_dw.c	(revision 1238)
@@ -83,5 +83,5 @@
 	
 	/* Now send this message */
-	CHECK_FCT( fd_out_send(&msg, NULL, peer) );
+	CHECK_FCT( fd_out_send(&msg, NULL, peer, 0) );
 	
 	/* And mark the pending DW */
@@ -106,5 +106,5 @@
 		CHECK_FCT( fd_msg_rescode_set( *msg, "DIAMETER_SUCCESS", NULL, NULL, 0 ) );
 		CHECK_FCT( fd_msg_add_origin ( *msg, 1 ) );
-		CHECK_FCT( fd_out_send( msg, peer->p_cnxctx, peer) );
+		CHECK_FCT( fd_out_send( msg, peer->p_cnxctx, peer, 0) );
 		
 	} else {
Index: libfdcore/p_out.c
===================================================================
--- libfdcore/p_out.c	(revision 1207)
+++ libfdcore/p_out.c	(revision 1238)
@@ -160,5 +160,5 @@
 
 /* Wrapper to sending a message either by out thread (peer in OPEN state) or directly; cnx or peer must be provided. Flags are valid only for direct sending, not through thread (unused) */
-int fd_out_send(struct msg ** msg, struct cnxctx * cnx, struct fd_peer * peer)
+int fd_out_send(struct msg ** msg, struct cnxctx * cnx, struct fd_peer * peer, int update_reqin_cnt)
 {
 	struct msg_hdr * hdr;
@@ -167,5 +167,5 @@
 	CHECK_PARAMS( msg && *msg && (cnx || (peer && peer->p_cnxctx)));
 	
-	if (peer) {
+	if (update_reqin_cnt && peer) {
 		CHECK_FCT( fd_msg_hdr(*msg, &hdr) );
 		if (!(hdr->msg_flags & CMD_FLAG_REQUEST)) {
Index: libfdcore/p_psm.c
===================================================================
--- libfdcore/p_psm.c	(revision 1198)
+++ libfdcore/p_psm.c	(revision 1238)
@@ -527,9 +527,4 @@
 			CHECK_FCT_DO( fd_msg_answ_associate( msg, req ), goto psm_end );
 			
-		} else {
-			/* Mark the incoming request so that we know we have pending answers for this peer */
-			CHECK_POSIX_DO( pthread_mutex_lock(&peer->p_state_mtx), goto psm_end  );
-			peer->p_reqin_count++;
-			CHECK_POSIX_DO( pthread_mutex_unlock(&peer->p_state_mtx), goto psm_end  );
 		}
 		
@@ -560,4 +555,11 @@
 					CHECK_FCT_DO( fd_msg_source_setrr( msg, peer->p_hdr.info.pi_diamid, peer->p_hdr.info.pi_diamidlen, fd_g_config->cnf_dict ), goto psm_end);
 
+					if ((hdr->msg_flags & CMD_FLAG_REQUEST)) {
+						/* Mark the incoming request so that we know we have pending answers for this peer */
+						CHECK_POSIX_DO( pthread_mutex_lock(&peer->p_state_mtx), goto psm_end  );
+						peer->p_reqin_count++;
+						CHECK_POSIX_DO( pthread_mutex_unlock(&peer->p_state_mtx), goto psm_end  );
+					}
+						
 					/* Requeue to the global incoming queue */
 					CHECK_FCT_DO(fd_fifo_post(fd_g_incoming, &msg), goto psm_end );
@@ -601,5 +603,5 @@
 				if (msg == NULL) {
 					/* Send the error back to the peer */
-					CHECK_FCT_DO( ret = fd_out_send(&error, NULL, peer),  );
+					CHECK_FCT_DO( ret = fd_out_send(&error, NULL, peer, 0),  );
 					if (error) {
 						char buf[256];
@@ -655,5 +657,5 @@
 
 						/* Send the answer */
-						CHECK_FCT_DO( fd_out_send(&msg, peer->p_cnxctx, peer), break );
+						CHECK_FCT_DO( fd_out_send(&msg, peer->p_cnxctx, peer, 0), break );
 					} while (0);
 				} else {
Index: libfdcore/p_sr.c
===================================================================
--- libfdcore/p_sr.c	(revision 1232)
+++ libfdcore/p_sr.c	(revision 1238)
@@ -337,4 +337,5 @@
 	/* The list of expiring requests must be empty now */
 	ASSERT( FD_IS_LIST_EMPTY(&srlist->exp) );
+	ASSERT( srlist->cnt == 0 ); /* debug the counter management if needed */
 	
 	CHECK_POSIX_DO( pthread_mutex_unlock(&srlist->mtx), /* continue anyway */ );
Index: libfdcore/peers.c
===================================================================
--- libfdcore/peers.c	(revision 1207)
+++ libfdcore/peers.c	(revision 1238)
@@ -550,5 +550,5 @@
 		fd_hook_call(HOOK_PEER_CONNECT_FAILED, *cer, NULL, "Received CER with invalid Origin-Host AVP", NULL);
 		
-		CHECK_FCT( fd_out_send(cer, *cnx, NULL) );
+		CHECK_FCT( fd_out_send(cer, *cnx, NULL, 0) );
 		return EINVAL;
 	}
Index: libfdcore/routing_dispatch.c
===================================================================
--- libfdcore/routing_dispatch.c	(revision 1230)
+++ libfdcore/routing_dispatch.c	(revision 1238)
@@ -415,5 +415,5 @@
 		CHECK_FCT( fd_fifo_post(fd_g_incoming, pmsg) );
 	} else {
-		CHECK_FCT( fd_out_send(pmsg, NULL, peer) );
+		CHECK_FCT( fd_out_send(pmsg, NULL, peer, 1) );
 	}
 	
@@ -893,5 +893,5 @@
 
 		/* Push the message into this peer */
-		CHECK_FCT( fd_out_send(&msgptr, NULL, peer) );
+		CHECK_FCT( fd_out_send(&msgptr, NULL, peer, 1) );
 
 		/* We're done with this answer */
@@ -1008,5 +1008,5 @@
 		if (fd_peer_getstate(peer) == STATE_OPEN) {
 			/* Send to this one */
-			CHECK_FCT_DO( fd_out_send(&msgptr, NULL, peer), continue );
+			CHECK_FCT_DO( fd_out_send(&msgptr, NULL, peer, 1), continue );
 			
 			/* If the sending was successful */
