Index: extensions/app_radgw/radius.c
===================================================================
--- extensions/app_radgw/radius.c	(revision 979)
+++ extensions/app_radgw/radius.c	(revision 996)
@@ -57,8 +57,4 @@
 /*********************************************************************************/
 #include "rgw.h"
-
-/* Overwrite printf */
-#define printf(args...) fd_log_debug(args)
-
 
 static struct radius_attr_hdr *
@@ -237,15 +233,17 @@
 }
 
-
-static void print_char(char c)
+static char print_char_buf[5];
+
+static char * print_char(char c)
 {
 	if (c >= 32 && c < 127)
-		printf("%c", c);
+		sprintf(print_char_buf, "%c", c);
 	else
-		printf("<%02x>", c);
-}
-
-
-static void radius_msg_dump_attr_val(struct radius_attr_hdr *hdr)
+		sprintf(print_char_buf, "<%02x>", c);
+	return print_char_buf;
+}
+
+
+static char * radius_msg_dump_attr_val(struct radius_attr_hdr *hdr, char * outbuf, size_t buflen)
 {
 	struct radius_attr_type *attr;
@@ -253,4 +251,7 @@
 	unsigned char *pos;
 	u8 attrtype;
+	size_t offset = 0;
+	
+	memset(outbuf, 0, buflen);
 
 	attr = radius_get_attr_type(hdr->type);
@@ -266,8 +267,8 @@
 	switch (attrtype) {
 	case RADIUS_ATTR_TEXT:
-		printf("      Value: '");
+		snprintf(outbuf + strlen(outbuf), buflen - strlen(outbuf), "      Value: '");
 		for (i = 0; i < len; i++)
-			print_char(pos[i]);
-		printf("'\n");
+			snprintf(outbuf + strlen(outbuf), buflen - strlen(outbuf), "%s", print_char(pos[i]));
+		snprintf(outbuf + strlen(outbuf), buflen - strlen(outbuf), "'");
 		break;
 
@@ -276,7 +277,7 @@
 			struct in_addr addr;
 			os_memcpy(&addr, pos, 4);
-			printf("      Value: %s\n", inet_ntoa(addr));
+			snprintf(outbuf + strlen(outbuf), buflen - strlen(outbuf), "      Value: %s", inet_ntoa(addr));
 		} else
-			printf("      Invalid IP address length %d\n", len);
+			snprintf(outbuf + strlen(outbuf), buflen - strlen(outbuf), "      Invalid IP address length %d", len);
 		break;
 
@@ -287,14 +288,14 @@
 			struct in6_addr *addr = (struct in6_addr *) pos;
 			atxt = inet_ntop(AF_INET6, addr, buf, sizeof(buf));
-			printf("      Value: %s\n", atxt ? atxt : "?");
+			snprintf(outbuf + strlen(outbuf), buflen - strlen(outbuf), "      Value: %s", atxt ? atxt : "?");
 		} else
-			printf("      Invalid IPv6 address length %d\n", len);
+			snprintf(outbuf + strlen(outbuf), buflen - strlen(outbuf), "      Invalid IPv6 address length %d", len);
 		break;
 
 	case RADIUS_ATTR_INT32:
 		if (len == 4)
-			printf("      Value: %u\n", WPA_GET_BE32(pos));
+			snprintf(outbuf + strlen(outbuf), buflen - strlen(outbuf), "      Value: %u", WPA_GET_BE32(pos));
 		else
-			printf("      Invalid INT32 length %d\n", len);
+			snprintf(outbuf + strlen(outbuf), buflen - strlen(outbuf), "      Invalid INT32 length %d", len);
 		break;
 
@@ -302,10 +303,11 @@
 	case RADIUS_ATTR_UNDIST:
 	default:
-		printf("      Value:");
+		snprintf(outbuf + strlen(outbuf), buflen - strlen(outbuf), "      Value:");
 		for (i = 0; i < len; i++)
-			printf(" %02x", pos[i]);
-		printf("\n");
+			snprintf(outbuf + strlen(outbuf), buflen - strlen(outbuf), " %02x", pos[i]);
 		break;
 	}
+	
+	return outbuf;
 }
 
@@ -314,4 +316,5 @@
 {
 	unsigned char *auth;
+	char buf[256];
 	size_t i;
 	if (! TRACE_BOOL(FULL) )
@@ -329,5 +332,5 @@
 		struct radius_attr_hdr *attr = (struct radius_attr_hdr *)(msg->radius.buf + msg->radius.attr_pos[i]);
 		fd_log_debug("    - Type: 0x%02hhx (%s)       Len: %-3hhu", attr->type, rgw_msg_attrtype_str(attr->type), attr->length);
-		radius_msg_dump_attr_val(attr);
+		fd_log_debug("%s", radius_msg_dump_attr_val(attr, buf, sizeof(buf)));
 	}
 	if (has_meta && msg->ps_nb) {
@@ -336,5 +339,5 @@
 			struct radius_attr_hdr *attr = (struct radius_attr_hdr *)(msg->radius.buf + msg->radius.attr_pos[i]);
 			fd_log_debug("    - Type: 0x%02hhx (%s)       Len: %-3hhu", attr->type, rgw_msg_attrtype_str(attr->type), attr->length);
-			radius_msg_dump_attr_val(attr);
+			fd_log_debug("%s", radius_msg_dump_attr_val(attr, buf, sizeof(buf)));
 		}
 	}
@@ -355,5 +358,5 @@
 					   auth, MD5_MAC_LEN);
 		if (attr == NULL) {
-			printf("WARNING: Could not add Message-Authenticator\n");
+			fd_log_debug("WARNING: Could not add Message-Authenticator");
 			return -1;
 		}
@@ -365,5 +368,5 @@
 
 	if (msg->buf_used > 0xffff) {
-		printf("WARNING: too long RADIUS message (%lu)\n",
+		fd_log_debug("WARNING: too long RADIUS message (%lu)",
 		       (unsigned long) msg->buf_used);
 		return -1;
@@ -386,5 +389,5 @@
 				       auth, MD5_MAC_LEN);
 	    if (attr == NULL) {
-		    printf("WARNING: Could not add Message-Authenticator\n");
+		    fd_log_debug("WARNING: Could not add Message-Authenticator");
 		    return -1;
 	    }
@@ -410,5 +413,5 @@
 
 	if (msg->buf_used > 0xffff) {
-		printf("WARNING: too long RADIUS message (%lu)\n",
+		fd_log_debug("WARNING: too long RADIUS message (%lu)",
 		       (unsigned long) msg->buf_used);
 		return -1;
@@ -433,5 +436,5 @@
 
 	if (msg->buf_used > 0xffff) {
-		printf("WARNING: too long RADIUS messages (%lu)\n",
+		fd_log_debug("WARNING: too long RADIUS messages (%lu)",
 		       (unsigned long) msg->buf_used);
 	}
@@ -468,5 +471,5 @@
 
 	if (data_len > RADIUS_MAX_ATTR_LEN) {
-		printf("radius_msg_add_attr: too long attribute (%lu bytes)\n",
+		fd_log_debug("radius_msg_add_attr: too long attribute (%lu bytes)",
 		       (unsigned long) data_len);
 		return NULL;
@@ -692,5 +695,5 @@
 		if (tmp->type == RADIUS_ATTR_MESSAGE_AUTHENTICATOR) {
 			if (attr != NULL) {
-				printf("Multiple Message-Authenticator attributes in RADIUS message\n");
+				fd_log_debug("Multiple Message-Authenticator attributes in RADIUS message");
 				return 1;
 			}
@@ -700,5 +703,5 @@
 
 	if (attr == NULL) {
-		printf("No Message-Authenticator attribute found\n");
+		fd_log_debug("No Message-Authenticator attribute found");
 		return 1;
 	}
@@ -720,5 +723,5 @@
 
 	if (os_memcmp(orig, auth, MD5_MAC_LEN) != 0) {
-		printf("Invalid Message-Authenticator!\n");
+		fd_log_debug("Invalid Message-Authenticator!");
 		return 1;
 	}
@@ -736,5 +739,5 @@
 
 	if (sent_msg == NULL) {
-		printf("No matching Access-Request message found\n");
+		fd_log_debug("No matching Access-Request message found");
 		return 1;
 	}
@@ -757,5 +760,5 @@
 	md5_vector(4, addr, len, hash);
 	if (os_memcmp(hash, msg->hdr->authenticator, MD5_MAC_LEN) != 0) {
-		printf("Response Authenticator invalid!\n");
+		fd_log_debug("Response Authenticator invalid!");
 		return 1;
 	}
@@ -895,5 +898,5 @@
 	left = len - 2;
 	if (left % 16) {
-		printf("Invalid ms key len %lu\n", (unsigned long) left);
+		fd_log_debug("Invalid ms key len %lu", (unsigned long) left);
 		return NULL;
 	}
@@ -929,5 +932,5 @@
 
 	if (plain[0] == 0 || plain[0] > plen - 1) {
-		printf("Failed to decrypt MPPE key\n");
+		fd_log_debug("Failed to decrypt MPPE key");
 		os_free(plain);
 		return NULL;
Index: extensions/app_radgw/rgw.h
===================================================================
--- extensions/app_radgw/rgw.h	(revision 741)
+++ extensions/app_radgw/rgw.h	(revision 996)
@@ -116,6 +116,6 @@
 void rgw_plg_dump(void);
 void rgw_plg_start_cache(void);
-int rgw_plg_loop_req(struct rgw_radius_msg_meta **rad, struct session **session, struct msg **diam_msg, struct rgw_client * cli);
-int rgw_plg_loop_ans(struct rgw_radius_msg_meta *req, struct session *session, struct msg **diam_ans, struct radius_msg ** rad_ans, struct rgw_client * cli, int * stateful);
+int rgw_plg_loop_req(struct rgw_radius_msg_meta **rad, struct msg **diam_msg, struct rgw_client * cli);
+int rgw_plg_loop_ans(struct rgw_radius_msg_meta *req, struct msg **diam_ans, struct radius_msg ** rad_ans, struct rgw_client * cli);
 void rgw_plg_fini(void);
 
Index: extensions/app_radgw/rgw_common.h
===================================================================
--- extensions/app_radgw/rgw_common.h	(revision 740)
+++ extensions/app_radgw/rgw_common.h	(revision 996)
@@ -74,5 +74,5 @@
 
 	/* handle an incoming RADIUS message */
-	int	(*rgwp_rad_req) ( struct rgwp_config * conf, struct session ** session, struct radius_msg * rad_req, struct radius_msg ** rad_ans, struct msg ** diam_fw, struct rgw_client * cli );
+	int	(*rgwp_rad_req) ( struct rgwp_config * conf, struct radius_msg * rad_req, struct radius_msg ** rad_ans, struct msg ** diam_fw, struct rgw_client * cli );
 	/* ret >0: critical error (errno), log and exit.
 	   ret 0: continue; 
@@ -82,7 +82,6 @@
 	
 	/* handle the corresponding Diameter answer */
-	int	(*rgwp_diam_ans) ( struct rgwp_config * conf, struct session * session, struct msg ** diam_ans, struct radius_msg ** rad_fw, struct rgw_client * cli, int * stateful );
+	int	(*rgwp_diam_ans) ( struct rgwp_config * conf, struct msg ** diam_ans, struct radius_msg ** rad_fw, struct rgw_client * cli );
 	/* ret 0: continue; ret >0: error; ret: -1 ... (tbd) */
-	/* if *stateful = 1 on return, the session will not be destroyed after RADIUS answer is sent. The extension must ensure to register a timeout on the session in this case. */
 	
 } rgwp_descriptor;
Index: extensions/app_radgw/rgw_plugins.c
===================================================================
--- extensions/app_radgw/rgw_plugins.c	(revision 974)
+++ extensions/app_radgw/rgw_plugins.c	(revision 996)
@@ -328,5 +328,5 @@
 }
 
-int rgw_plg_loop_req(struct rgw_radius_msg_meta **rad, struct session **session, struct msg **diam_msg, struct rgw_client * cli)
+int rgw_plg_loop_req(struct rgw_radius_msg_meta **rad, struct msg **diam_msg, struct rgw_client * cli)
 {
 	int ret = 0;
@@ -334,6 +334,6 @@
 	struct radius_msg * rad_ans = NULL;
 	
-	TRACE_ENTRY("%p %p %p %p", rad, session, diam_msg, cli);
-	CHECK_PARAMS( rad && *rad && session && diam_msg && *diam_msg && cli);
+	TRACE_ENTRY("%p %p %p", rad, diam_msg, cli);
+	CHECK_PARAMS( rad && *rad && diam_msg && *diam_msg && cli);
 	
 	/* First, get the list of extensions for this message */
@@ -348,5 +348,5 @@
 		if (plg->descriptor->rgwp_rad_req) {
 			TRACE_DEBUG(ANNOYING, "Calling next plugin: %s", plg->descriptor->rgwp_name);
-			ret = (*plg->descriptor->rgwp_rad_req)(plg->cs, session, &(*rad)->radius, &rad_ans, diam_msg, cli);
+			ret = (*plg->descriptor->rgwp_rad_req)(plg->cs, &(*rad)->radius, &rad_ans, diam_msg, cli);
 			if (ret)
 				break;
@@ -368,9 +368,4 @@
 	}
 	
-	/* Destroy the session, there won't be a reply message to retrieve the data */
-	if (*session) {
-		CHECK_FCT_DO( fd_sess_destroy(session), );
-	}
-	
 	/* Send the radius message back if required */
 	if ((ret == -2) && rad_ans && rad) {
@@ -390,13 +385,11 @@
 
 /* Loop in the extension list (same as req) to convert data from diam_ans to rad_ans */
-int rgw_plg_loop_ans(struct rgw_radius_msg_meta *req, struct session *session, struct msg **diam_ans, struct radius_msg ** rad_ans, struct rgw_client * cli, int * stateful)
+int rgw_plg_loop_ans(struct rgw_radius_msg_meta *req, struct msg **diam_ans, struct radius_msg ** rad_ans, struct rgw_client * cli)
 {
 	int ret = 0;
 	struct fd_list * head = NULL, *li;
 	
-	TRACE_ENTRY("%p %p %p %p %p %p", req, session, diam_ans, rad_ans, cli, stateful);
-	CHECK_PARAMS( req && session && diam_ans && *diam_ans && rad_ans && *rad_ans && cli && stateful);
-	
-	*stateful = 0; /* default: stateless gateway */
+	TRACE_ENTRY("%p %p %p %p", req, diam_ans, rad_ans, cli);
+	CHECK_PARAMS( req && diam_ans && *diam_ans && rad_ans && *rad_ans && cli);
 	
 	/* Get the list of extensions of the RADIUS request */
@@ -412,8 +405,7 @@
 		if (plg->descriptor->rgwp_diam_ans) {
 			TRACE_DEBUG(ANNOYING, "Calling next plugin: %s", plg->descriptor->rgwp_name);
-			ret = (*plg->descriptor->rgwp_diam_ans)(plg->cs, session, diam_ans, rad_ans, (void *)cli, &locstateful);
+			ret = (*plg->descriptor->rgwp_diam_ans)(plg->cs, diam_ans, rad_ans, (void *)cli);
 			if (ret)
 				break;
-			*stateful |= locstateful;
 		} else {
 			TRACE_DEBUG(ANNOYING, "Skipping extension '%s' (NULL callback)", plg->descriptor->rgwp_name);
Index: extensions/app_radgw/rgw_servers.c
===================================================================
--- extensions/app_radgw/rgw_servers.c	(revision 974)
+++ extensions/app_radgw/rgw_servers.c	(revision 996)
@@ -133,6 +133,9 @@
 		}
 		
-		TRACE_DEBUG(FULL, "Received %d bytes", len);
-		TRACE_sSA(FD_LOG_DEBUG, FULL, " from ", &from, NI_NUMERICHOST | NI_NUMERICSERV, "" );
+		{
+			char __buf[1024];
+			sSA_DUMP_NODE_SERV(__buf, sizeof(__buf), &from, NI_NUMERICHOST | NI_NUMERICSERV );
+			TRACE_DEBUG(FULL, "Received %d bytes from %s", len, __buf);
+		}
 		
 		/* Search the associated client definition, if any */
@@ -273,6 +276,9 @@
 	}
 	
-	TRACE_DEBUG(FULL, "Sending %d bytes", buflen);
-	TRACE_sSA(FD_LOG_DEBUG, FULL, " to ", &sto, NI_NUMERICHOST | NI_NUMERICSERV, "" );
+	{
+		char __buf[1024];
+		sSA_DUMP_NODE_SERV(__buf, sizeof(__buf), &sto, NI_NUMERICHOST | NI_NUMERICSERV );
+		TRACE_DEBUG(FULL, "Sending %d bytes to %s", buflen, __buf);
+	}
 		
 	/* Send */
Index: extensions/app_radgw/rgw_worker.c
===================================================================
--- extensions/app_radgw/rgw_worker.c	(revision 979)
+++ extensions/app_radgw/rgw_worker.c	(revision 996)
@@ -54,5 +54,4 @@
 	struct rgw_radius_msg_meta * rad;  /* the RADIUS message that was received and translated */
 	struct rgw_client          * cli;  /* the client it was received from */
-	struct session 		   * sess; /* the Diameter session created for this message (useful?) */
 };
 
@@ -76,5 +75,4 @@
 		struct rgw_radius_msg_meta * msg;
 		struct rgw_client * cli;
-		struct session * session;
 		struct msg * diam_msg;
 		int pb, a;
@@ -133,8 +131,6 @@
 			}  );
 		
-		session = NULL;
-		
 		/* Pass the message to the list of registered plugins */
-		CHECK_FCT_DO( rgw_plg_loop_req(&msg, &session, &diam_msg, cli), 
+		CHECK_FCT_DO( rgw_plg_loop_req(&msg, &diam_msg, cli), 
 			{
 				/* An error occurred, discard message */
@@ -142,7 +138,4 @@
 					CHECK_FCT_DO( fd_msg_free(diam_msg), );
 					diam_msg = NULL;
-				}
-				if (session) {
-					CHECK_FCT_DO( fd_sess_destroy(&session), );
 				}
 				rgw_msg_free(&msg);
@@ -181,8 +174,4 @@
 		if (pb) {
 			/* Something went wrong during the conversion */
-			if (session) {
-				CHECK_FCT_DO( fd_sess_destroy(&session), );
-			}
-			
 			if (diam_msg) {
 				CHECK_FCT_DO( fd_msg_free(diam_msg), );
@@ -202,5 +191,4 @@
 		pa->rad = msg;
 		pa->cli = cli;
-		pa->sess= session;
 		
 		CHECK_FCT_DO( fd_msg_send( &diam_msg, receive_diam_answer, pa), 
@@ -208,7 +196,4 @@
 				/* If an error occurs, log and destroy the data */
 				fd_log_debug("An error occurred while sending Diameter message, please turn Debug on for detail.");
-				if (session) {
-					CHECK_FCT_DO( fd_sess_destroy(&session), );
-				}
 
 				if (diam_msg) {
@@ -239,5 +224,4 @@
 	struct avp_hdr  *ahdr;
 	int pb = 0;
-	int keepsession=0;
 	
 	TRACE_ENTRY("%p %p", pa, ans);
@@ -248,5 +232,5 @@
 	
 	/* Pass the Diameter answer to the same extensions as the request */
-	CHECK_FCT_DO( rgw_plg_loop_ans(pa->rad, pa->sess, ans, &rad_ans, pa->cli, &keepsession), goto out );
+	CHECK_FCT_DO( rgw_plg_loop_ans(pa->rad, ans, &rad_ans, pa->cli), goto out );
 
 	if (*ans != NULL) {
@@ -300,9 +284,4 @@
 	}
 	
-	if (!keepsession) {
-		/* Destroy remaining session data (stateless gateway) */
-		CHECK_FCT_DO( fd_sess_destroy(&pa->sess),  );
-	}
-	
 	/* Clear the RADIUS request */
 	if (pa->rad) {
Index: extensions/app_radgw/rgwx_acct.c
===================================================================
--- extensions/app_radgw/rgwx_acct.c	(revision 974)
+++ extensions/app_radgw/rgwx_acct.c	(revision 996)
@@ -289,5 +289,5 @@
 
 /* Incoming RADIUS request */
-static int acct_rad_req( struct rgwp_config * cs, struct session ** session, struct radius_msg * rad_req, struct radius_msg ** rad_ans, struct msg ** diam_fw, struct rgw_client * cli )
+static int acct_rad_req( struct rgwp_config * cs, struct radius_msg * rad_req, struct radius_msg ** rad_ans, struct msg ** diam_fw, struct rgw_client * cli )
 {
 	int idx;
@@ -303,4 +303,5 @@
 	union avp_value value;
 	struct avp ** avp_tun = NULL, *avp = NULL;
+	struct session * sess;
 	
 	const char * prefix = "Diameter/";
@@ -311,5 +312,5 @@
 	size_t un_len = 0;
 	
-	TRACE_ENTRY("%p %p %p %p %p %p", cs, session, rad_req, rad_ans, diam_fw, cli);
+	TRACE_ENTRY("%p %p %p %p %p", cs, rad_req, rad_ans, diam_fw, cli);
 	CHECK_PARAMS(rad_req && (rad_req->hdr->code == RADIUS_CODE_ACCOUNTING_REQUEST) && rad_ans && diam_fw && *diam_fw);
 	
@@ -452,10 +453,10 @@
 	if (status_type == RADIUS_ACCT_STATUS_TYPE_ACCOUNTING_OFF) {
 		TRACE_DEBUG(FULL, "[acct.rgwx] Received Accounting-Off Acct-Status-Type attribute, we must terminate all active sessions.");
-		TODO("RADIUS side is rebooting, send STR on all sessions?");
+		TODO("RADIUS side is rebooting, send STR on all sessions???");
 		return ENOTSUP;
 	}
 	
 	/* Check if we got a valid session information, otherwise the server will not be able to handle the data... */
-	if (!*session && !si) {
+	if (!si) {
 		TRACE_DEBUG(INFO, "[acct.rgwx] RADIUS Account-Request from %s did not contain a CLASS attribute with Diameter session information, reject.", rgw_clients_id(cli));
 		return EINVAL;
@@ -483,9 +484,9 @@
 	}
 	CHECK_FCT( fd_msg_avp_setvalue ( avp, &value ) );
-	CHECK_FCT( fd_msg_avp_add ( *diam_fw, *session ? MSG_BRW_LAST_CHILD : MSG_BRW_FIRST_CHILD, avp) );
-	
-	/* Create the Session-Id AVP if needed */
-	if (!*session) {
-		CHECK_FCT( fd_sess_fromsid ( si, si_len, session, NULL) );
+	CHECK_FCT( fd_msg_avp_add ( *diam_fw, MSG_BRW_FIRST_CHILD, avp) );
+	
+	/* Create the Session-Id AVP */
+	{
+		CHECK_FCT( fd_sess_fromsid_msg ( si, si_len, &sess, NULL) );
 		
 		TRACE_DEBUG(FULL, "[acct.rgwx] Translating new accounting message for session '%.*s'...", si_len, si);
@@ -497,4 +498,5 @@
 		CHECK_FCT( fd_msg_avp_setvalue ( avp, &value ) );
 		CHECK_FCT( fd_msg_avp_add ( *diam_fw, MSG_BRW_FIRST_CHILD, avp) );
+		CHECK_FCT( fd_msg_sess_set(*diam_fw, sess) );
 	}
 	
@@ -1177,5 +1179,4 @@
 	{
 		struct sess_state * st;
-		CHECK_PARAMS(session);
 		
 		CHECK_MALLOC( st = malloc(sizeof(struct sess_state)) );
@@ -1186,5 +1187,5 @@
 		}
 		st->term_cause = str_cause;
-		CHECK_FCT( fd_sess_state_store( cs->sess_hdl, *session, &st ) );
+		CHECK_FCT( fd_sess_state_store( cs->sess_hdl, sess, &st ) );
 	}
 	
@@ -1222,15 +1223,20 @@
 }
 
-static int acct_diam_ans( struct rgwp_config * cs, struct session * session, struct msg ** diam_ans, struct radius_msg ** rad_fw, struct rgw_client * cli, int * stateful )
+static int acct_diam_ans( struct rgwp_config * cs, struct msg ** diam_ans, struct radius_msg ** rad_fw, struct rgw_client * cli )
 {
+	struct session * sess;
 	struct sess_state * st = NULL, stloc;
 	struct avp *avp, *next;
-	struct avp_hdr *ahdr, *sid, *oh, *or;
-	
-	TRACE_ENTRY("%p %p %p %p %p", cs, session, diam_ans, rad_fw, cli);
+	struct avp_hdr *ahdr, *oh, *or;
+	os0_t sid = NULL;
+	size_t sidlen;
+	
+	TRACE_ENTRY("%p %p %p %p", cs, diam_ans, rad_fw, cli);
 	CHECK_PARAMS(cs);
 	
-	if (session) {
-		CHECK_FCT( fd_sess_state_retrieve( cs->sess_hdl, session, &st ) );
+	CHECK_FCT( fd_msg_sess_get(fd_g_config->cnf_dict, *diam_ans, &sess, NULL) );
+	if (sess) {
+		CHECK_FCT( fd_sess_state_retrieve( cs->sess_hdl, sess, &st ) );
+		CHECK_FCT( fd_sess_getsid(sess, &sid, &sidlen) );
 	}
 	
@@ -1246,7 +1252,4 @@
 	
 	/* Search these AVPs first */
-	CHECK_FCT( fd_msg_search_avp (*diam_ans, cs->dict.Session_Id, &avp) );
-	CHECK_FCT( fd_msg_avp_hdr ( avp, &sid ) );
-	
 	CHECK_FCT( fd_msg_search_avp (*diam_ans, cs->dict.Origin_Host, &avp) );
 	CHECK_FCT( fd_msg_avp_hdr ( avp, &oh ) );
@@ -1269,5 +1272,5 @@
 					ahdr->avp_value->u32, 
 					oh->avp_value->os.len, oh->avp_value->os.data,
-					sid->avp_value->os.len, sid->avp_value->os.data);
+					sidlen, sid);
 			CHECK_FCT( fd_msg_search_avp (*diam_ans, cs->dict.Error_Message, &avp) );
 			if (avp) {
@@ -1310,5 +1313,5 @@
 		CHECK_FCT(  fd_msg_new ( cs->dict.Session_Termination_Request, MSGFL_ALLOC_ETEID, &str )  );
 		
-		/* Set the application-id to the auth application if available, accouting otherwise (not sure what is actually expected...) */
+		/* Set the application-id to the auth application if available, accounting otherwise (not sure what is actually expected...) */
 		CHECK_FCT( fd_msg_hdr ( str, &hdr ) );
 		hdr->msg_appl = st->auth_appl ?: AI_ACCT;
@@ -1316,6 +1319,9 @@
 		/* Add the Session-Id AVP as first AVP */
 		CHECK_FCT( fd_msg_avp_new (  cs->dict.Session_Id, 0, &avp ) );
-		CHECK_FCT( fd_msg_avp_setvalue ( avp, sid->avp_value ) );
+		avp_val.os.data = sid;
+		avp_val.os.len = sidlen;
+		CHECK_FCT( fd_msg_avp_setvalue ( avp, &avp_val ) );
 		CHECK_FCT( fd_msg_avp_add ( str, MSG_BRW_FIRST_CHILD, avp) );
+		CHECK_FCT( fd_sess_ref_msg(sess) );
 
 		/* Add the Destination-Realm as next AVP */
Index: extensions/app_radgw/rgwx_auth.c
===================================================================
--- extensions/app_radgw/rgwx_auth.c	(revision 979)
+++ extensions/app_radgw/rgwx_auth.c	(revision 996)
@@ -229,5 +229,5 @@
 
 /* Handle an incoming RADIUS request */
-static int auth_rad_req( struct rgwp_config * cs, struct session ** session, struct radius_msg * rad_req, struct radius_msg ** rad_ans, struct msg ** diam_fw, struct rgw_client * cli )
+static int auth_rad_req( struct rgwp_config * cs, struct radius_msg * rad_req, struct radius_msg ** rad_ans, struct msg ** diam_fw, struct rgw_client * cli )
 {
 	int idx;
@@ -250,7 +250,8 @@
 	struct avp ** avp_tun = NULL, *avp = NULL;
 	union avp_value value;
-	
-	TRACE_ENTRY("%p %p %p %p %p %p", cs, session, rad_req, rad_ans, diam_fw, cli);
-	CHECK_PARAMS(cs && session && rad_req && (rad_req->hdr->code == RADIUS_CODE_ACCESS_REQUEST) && rad_ans && diam_fw && *diam_fw);
+	struct session * sess;
+	
+	TRACE_ENTRY("%p %p %p %p %p", cs, rad_req, rad_ans, diam_fw, cli);
+	CHECK_PARAMS(cs && rad_req && (rad_req->hdr->code == RADIUS_CODE_ACCESS_REQUEST) && rad_ans && diam_fw && *diam_fw);
 	
 	pref_len = strlen(prefix);
@@ -440,5 +441,5 @@
 	}
 	CHECK_FCT( fd_msg_avp_setvalue ( avp, &value ) );
-	CHECK_FCT( fd_msg_avp_add ( *diam_fw, *session ? MSG_BRW_LAST_CHILD : MSG_BRW_FIRST_CHILD, avp) );
+	CHECK_FCT( fd_msg_avp_add ( *diam_fw, MSG_BRW_FIRST_CHILD, avp) );
 	
 	/* Add the Destination-Host if found */
@@ -448,9 +449,9 @@
 		value.os.len = dh_len;
 		CHECK_FCT( fd_msg_avp_setvalue ( avp, &value ) );
-		CHECK_FCT( fd_msg_avp_add ( *diam_fw, *session ? MSG_BRW_LAST_CHILD : MSG_BRW_FIRST_CHILD, avp) );
+		CHECK_FCT( fd_msg_avp_add ( *diam_fw, MSG_BRW_FIRST_CHILD, avp) );
 	}
 	
 	/* Create the session if it is not already done */
-	if (*session == NULL) {
+	{
 		os0_t sess_str = NULL;
 		size_t sess_strlen;
@@ -458,5 +459,5 @@
 		if (si_len) {
 			/* We already have the Session-Id, just use it */
-			CHECK_FCT( fd_sess_fromsid_msg ( si, si_len, session, NULL) );
+			CHECK_FCT( fd_sess_fromsid_msg ( si, si_len, &sess, NULL) );
 		} else {
 			/* Create a new Session-Id string */
@@ -476,5 +477,5 @@
 				CHECK_MALLOC( sess_str = malloc(un_len + 1 /* ';' */ + fd_g_config->cnf_diamid_len + 1 /* '\0' */) );
 				len = sprintf((char *)sess_str, "%.*s;%s", (int)un_len, un, fd_g_config->cnf_diamid);
-				CHECK_FCT( fd_sess_new(session, fqdn, fqdnlen, sess_str, len) );
+				CHECK_FCT( fd_sess_new(&sess, fqdn, fqdnlen, sess_str, len) );
 				free(sess_str);
 			} else {
@@ -486,9 +487,8 @@
 		
 		/* Now, add the Session-Id AVP at beginning of Diameter message */
-		CHECK_FCT( fd_sess_getsid(*session, &sess_str, &sess_strlen) );
-		
+		CHECK_FCT( fd_sess_getsid(sess, &sess_str, &sess_strlen) );
 		TRACE_DEBUG(FULL, "[auth.rgwx] Translating new message for session '%s'...", sess_str);
 		
-		/* Add the Session-Id AVP as first AVP */
+		/* Now add this session in the message */
 		CHECK_FCT( fd_msg_avp_new ( cs->dict.Session_Id, 0, &avp ) );
 		value.os.data = sess_str;
@@ -496,5 +496,5 @@
 		CHECK_FCT( fd_msg_avp_setvalue ( avp, &value ) );
 		CHECK_FCT( fd_msg_avp_add ( *diam_fw, MSG_BRW_FIRST_CHILD, avp) );
-		CHECK_FCT( fd_msg_sess_set( *diam_fw, *session) );
+		CHECK_FCT( fd_msg_sess_set(*diam_fw, sess) );
 	}
 	
@@ -1056,10 +1056,10 @@
 
 	/* Store the request identifier in the session (if provided) */
-	if (*session) {
+	{
 		unsigned char * req_auth;
 		CHECK_MALLOC(req_auth = malloc(16));
 		memcpy(req_auth, &rad_req->hdr->authenticator[0], 16);
 		
-		CHECK_FCT( fd_sess_state_store( cs->sess_hdl, *session, &req_auth ) );
+		CHECK_FCT( fd_sess_state_store( cs->sess_hdl, sess, &req_auth ) );
 	}
 	
@@ -1067,9 +1067,9 @@
 }
 
-static int auth_diam_ans( struct rgwp_config * cs, struct session * session, struct msg ** diam_ans, struct radius_msg ** rad_fw, struct rgw_client * cli, int * stateful )
+static int auth_diam_ans( struct rgwp_config * cs, struct msg ** diam_ans, struct radius_msg ** rad_fw, struct rgw_client * cli )
 {
 	struct msg_hdr * hdr;
-	struct avp *avp, *next, *avp_x, *avp_y, *asid, *aoh;
-	struct avp_hdr *ahdr, *sid, *oh;
+	struct avp *avp, *next, *avp_x, *avp_y, *aoh;
+	struct avp_hdr *ahdr, *oh;
 	uint8_t buf[254]; /* to store some attributes values (with final '\0') */
 	size_t sz;
@@ -1079,12 +1079,17 @@
 	unsigned char * req_auth = NULL;
 	int error_cause = 0;
-	
-	TRACE_ENTRY("%p %p %p %p %p", cs, session, diam_ans, rad_fw, cli);
-	CHECK_PARAMS(cs && session && diam_ans && *diam_ans && rad_fw && *rad_fw);
+	struct session * sess;
+	os0_t sid = NULL;
+	size_t sidlen;
+	
+	TRACE_ENTRY("%p %p %p %p", cs, diam_ans, rad_fw, cli);
+	CHECK_PARAMS(cs && diam_ans && *diam_ans && rad_fw && *rad_fw);
 	
 	/* Retrieve the request identified which was stored in the session */
-	if (session) {
-		CHECK_FCT( fd_sess_state_retrieve( cs->sess_hdl, session, &req_auth ) );
-	}
+	CHECK_FCT( fd_msg_sess_get(fd_g_config->cnf_dict, *diam_ans, &sess, NULL) );
+	if (sess) {
+		CHECK_FCT( fd_sess_state_retrieve( cs->sess_hdl, sess, &req_auth ) );
+		CHECK_FCT( fd_sess_getsid(sess, &sid, &sidlen) );
+	} /* else ? */
 	
 	/*	
@@ -1145,6 +1150,4 @@
 
 	/* Search the different AVPs we handle here */
-	CHECK_FCT( fd_msg_search_avp (*diam_ans, cs->dict.Session_Id, &asid) );
-	CHECK_FCT( fd_msg_avp_hdr ( asid, &sid ) );
 	CHECK_FCT( fd_msg_search_avp (*diam_ans, cs->dict.Origin_Host, &aoh) );
 	CHECK_FCT( fd_msg_avp_hdr ( aoh, &oh ) );
@@ -1240,5 +1243,5 @@
 					ahdr->avp_value->u32, 
 					oh->avp_value->os.len, oh->avp_value->os.data,
-					sid->avp_value->os.len, sid->avp_value->os.data);
+					sidlen, sid);
 			CHECK_FCT( fd_msg_search_avp (*diam_ans, cs->dict.Error_Message, &avp_x) );
 			if (avp_x) {
@@ -1271,5 +1274,5 @@
 				(int)oh->avp_value->os.len,  (char *)oh->avp_value->os.data,
 				(int)ahdr->avp_value->os.len,  (char *)ahdr->avp_value->os.data,
-				(int)sid->avp_value->os.len, (char *)sid->avp_value->os.data))) {
+				(int)sidlen, (char *)sid))) {
 			TRACE_DEBUG(INFO, "Data truncated in State attribute: %s", buf);
 		}
@@ -1280,5 +1283,5 @@
 		/* Add the Session-Id */
 		if (sizeof(buf) < (sz = snprintf((char *)buf, sizeof(buf), "Diameter/%.*s", 
-				(int)sid->avp_value->os.len, sid->avp_value->os.data))) {
+				(int)sidlen, sid))) {
 			TRACE_DEBUG(INFO, "Data truncated in Class attribute: %s", buf);
 		}
@@ -1455,5 +1458,5 @@
 									((ahdr->avp_value->u32 == 2) ? "AUTHORIZE_ONLY" : "???"),
 								oh->avp_value->os.len, oh->avp_value->os.data, 
-								sid->avp_value->os.len, sid->avp_value->os.len);
+								sidlen, sid);
 					}
 					break;
@@ -1616,5 +1619,5 @@
 					fd_log_debug("[auth.rgwx] Received Diameter answer with non-translatable NAS-Filter-Rule AVP from '%.*s' (session: '%.*s'), ignoring.",
 							oh->avp_value->os.len, oh->avp_value->os.data,
-							sid->avp_value->os.len, sid->avp_value->os.data);
+							sidlen, sid);
 					handled = 0;
 					break;
@@ -1649,5 +1652,5 @@
 					fd_log_debug("[auth.rgwx] Received Diameter answer with non-translatable QoS-Filter-Rule AVP from '%.*s' (session: '%.*s'), ignoring.",
 							oh->avp_value->os.len, oh->avp_value->os.data,
-							sid->avp_value->os.len, sid->avp_value->os.data);
+							sidlen, sid);
 					handled = 0;
 					break;
@@ -1921,5 +1924,4 @@
 	}
 	
-	CHECK_FCT( fd_msg_free( asid ) );
 	CHECK_FCT( fd_msg_free( aoh ) );
 	free(req_auth);
@@ -1930,5 +1932,5 @@
 			return ENOMEM;
 		}
-	}		
+	}
 
 	if ((*rad_fw)->hdr->code == RADIUS_CODE_ACCESS_ACCEPT) {
Index: extensions/app_radgw/rgwx_debug.c
===================================================================
--- extensions/app_radgw/rgwx_debug.c	(revision 974)
+++ extensions/app_radgw/rgwx_debug.c	(revision 996)
@@ -72,7 +72,7 @@
 
 /* Function called when a new RADIUS message is being converted to Diameter */
-static int debug_rad_req( struct rgwp_config * cs, struct session ** session, struct radius_msg * rad_req, struct radius_msg ** rad_ans, struct msg ** diam_fw, struct rgw_client * cli )
+static int debug_rad_req( struct rgwp_config * cs, struct radius_msg * rad_req, struct radius_msg ** rad_ans, struct msg ** diam_fw, struct rgw_client * cli )
 {
-	TRACE_ENTRY("%p %p %p %p %p %p", cs, session, rad_req, rad_ans, diam_fw, cli);
+	TRACE_ENTRY("%p %p %p %p %p", cs, rad_req, rad_ans, diam_fw, cli);
 	
 	fd_log_debug("------------- RADIUS/Diameter Request Debug%s%s%s -------------", cs ? " [" : "", cs ? (char *)cs : "", cs ? "]" : "");
@@ -99,14 +99,4 @@
 	}
 	
-	if (!session || ! *session) {
-		fd_log_debug(" Diameter session: NULL pointer");
-	} else {
-		os0_t str;
-		size_t str_len;
-		CHECK_FCT( fd_sess_getsid(*session, &str, &str_len) );
-
-		fd_log_debug(" Diameter session: %s", str);
-	}
-	
 	fd_log_debug("===========  Debug%s%s%s complete =============", cs ? " [" : "", cs ? (char *)cs : "", cs ? "]" : "");
 	
@@ -115,7 +105,7 @@
 
 /* This one, when Diameter answer is converted to RADIUS */
-static int debug_diam_ans( struct rgwp_config * cs, struct session * session, struct msg ** diam_ans, struct radius_msg ** rad_fw, struct rgw_client * cli, int * stateful )
+static int debug_diam_ans( struct rgwp_config * cs, struct msg ** diam_ans, struct radius_msg ** rad_fw, struct rgw_client * cli )
 {
-	TRACE_ENTRY("%p %p %p %p %p %p", cs, session, diam_ans, rad_fw, cli, stateful);
+	TRACE_ENTRY("%p %p %p %p", cs, diam_ans, rad_fw, cli);
 
 	fd_log_debug("------------- RADIUS/Diameter Answer Debug%s%s%s -------------", cs ? " [" : "", cs ? (char *)cs : "", cs ? "]" : "");
Index: extensions/app_radgw/rgwx_echodrop.c
===================================================================
--- extensions/app_radgw/rgwx_echodrop.c	(revision 979)
+++ extensions/app_radgw/rgwx_echodrop.c	(revision 996)
@@ -119,14 +119,13 @@
 
 /* Handle attributes from a RADIUS request as specified in the configuration */
-static int ed_rad_req( struct rgwp_config * cs, struct session ** session, struct radius_msg * rad_req, struct radius_msg ** rad_ans, struct msg ** diam_fw, struct rgw_client * cli )
+static int ed_rad_req( struct rgwp_config * cs, struct radius_msg * rad_req, struct radius_msg ** rad_ans, struct msg ** diam_fw, struct rgw_client * cli )
 {
 	size_t nattr_used = 0;
 	int idx;
-	
 	struct fd_list echo_list = FD_LIST_INITIALIZER(echo_list);
 	struct fd_list *li;
 	
-	TRACE_ENTRY("%p %p %p %p %p %p", cs, session, rad_req, rad_ans, diam_fw, cli);
-	CHECK_PARAMS(cs && rad_req && session);
+	TRACE_ENTRY("%p %p %p %p %p", cs, rad_req, rad_ans, diam_fw, cli);
+	CHECK_PARAMS(cs && rad_req);
 	
 	/* For each attribute in the original message */
@@ -217,5 +216,9 @@
 	/* Save the echoed values in the session, if any */
 	if (!FD_IS_LIST_EMPTY(&echo_list)) {
-		CHECK_PARAMS_DO(*session,
+		struct session * sess;
+		
+		CHECK_FCT( fd_msg_sess_get(fd_g_config->cnf_dict, *diam_fw, &sess, NULL) );
+
+		CHECK_PARAMS_DO(sess,
 			{
 				fd_log_debug(	"[echodrop.rgwx] The extension is configured to echo some attributes from this message, but no session object has been created for it (yet)."
@@ -231,5 +234,5 @@
 		
 		/* Save the list in the session */
-		CHECK_FCT( fd_sess_state_store( cs->sess_hdl, *session, &li ) );
+		CHECK_FCT( fd_sess_state_store( cs->sess_hdl, sess, &li ) );
 	}
 	
@@ -238,13 +241,16 @@
 
 /* Process an answer: add the ECHO attributes back, if any */
-static int ed_diam_ans( struct rgwp_config * cs, struct session * session, struct msg ** diam_ans, struct radius_msg ** rad_fw, struct rgw_client * cli, int * stateful )
+static int ed_diam_ans( struct rgwp_config * cs, struct msg ** diam_ans, struct radius_msg ** rad_fw, struct rgw_client * cli )
 {
 	struct fd_list * list = NULL;
-	
-	TRACE_ENTRY("%p %p %p %p %p %p", cs, session, diam_ans, rad_fw, cli, stateful);
+	struct session * sess;
+	
+	TRACE_ENTRY("%p %p %p %p", cs, diam_ans, rad_fw, cli);
 	CHECK_PARAMS(cs);
 	
+	CHECK_FCT( fd_msg_sess_get(fd_g_config->cnf_dict, *diam_ans, &sess, NULL) );
+	
 	/* If there is no session associated, just give up */
-	if (! session ) {
+	if (! sess ) {
 		TRACE_DEBUG(FULL, "No session associated with the message, nothing to do here...");
 		return 0;
@@ -252,5 +258,5 @@
 	
 	/* Now try and retrieve any data from the session */
-	CHECK_FCT( fd_sess_state_retrieve( cs->sess_hdl, session, &list ) );
+	CHECK_FCT( fd_sess_state_retrieve( cs->sess_hdl, sess, &list ) );
 	if (list == NULL) {
 		/* No attribute saved in the session, just return */
Index: extensions/app_radgw/rgwx_sample.c
===================================================================
--- extensions/app_radgw/rgwx_sample.c	(revision 741)
+++ extensions/app_radgw/rgwx_sample.c	(revision 996)
@@ -70,7 +70,7 @@
 
 /* This function is called on incoming RADIUS messages. It should handle (some) RADIUS data and store into the Diameter message. */
-static int sample_rad_req( struct rgwp_config * cs, struct session ** session, struct radius_msg * rad_req, struct radius_msg ** rad_ans, struct msg ** diam_fw, struct rgw_client * cli )
+static int sample_rad_req( struct rgwp_config * cs, struct radius_msg * rad_req, struct radius_msg ** rad_ans, struct msg ** diam_fw, struct rgw_client * cli )
 {
-	TRACE_ENTRY("%p %p %p %p %p %p", cs, session, rad_req, rad_ans, diam_fw, cli);
+	TRACE_ENTRY("%p %p %p %p %p", cs, rad_req, rad_ans, diam_fw, cli);
 	CHECK_PARAMS(cs);
 	TRACE_DEBUG(INFO, "RADIUS/Diameter Sample plugin received a new RADIUS message.");
@@ -79,7 +79,7 @@
 
 /* This function is called when a Diameter answer is coming back. It should remove the AVPs and add the attributes in the RADIUS message. */
-static int sample_diam_ans( struct rgwp_config * cs, struct session * session, struct msg ** diam_ans, struct radius_msg ** rad_fw, struct rgw_client * cli, int * stateful )
+static int sample_diam_ans( struct rgwp_config * cs, struct msg ** diam_ans, struct radius_msg ** rad_fw, struct rgw_client * cli )
 {
-	TRACE_ENTRY("%p %p %p %p %p %p", cs, session, diam_ans, rad_fw, cli, stateful);
+	TRACE_ENTRY("%p %p %p %p", cs, diam_ans, rad_fw, cli);
 	CHECK_PARAMS(cs);
 	TRACE_DEBUG(INFO, "RADIUS/Diameter Sample plugin received a new Diameter answer.");
Index: extensions/app_radgw/rgwx_sip.c
===================================================================
--- extensions/app_radgw/rgwx_sip.c	(revision 974)
+++ extensions/app_radgw/rgwx_sip.c	(revision 996)
@@ -2,5 +2,6 @@
 * Software License Agreement (BSD License)                                                               *
 * Author: Alexandre Westfahl <awestfahl@freediameter.net>						 *
-*													 *
+*													 *												 *
+* Copyright (c) 2013, WIDE Project and NICT								 *
 * Copyright (c) 2010, Alexandre Westfahl, Teraoka Laboratory (Keio University), and the WIDE Project. 	 *		
 *													 *
@@ -322,5 +323,5 @@
 
 /* Handle an incoming RADIUS request */
-static int sip_rad_req( struct rgwp_config * cs, struct session ** session, struct radius_msg * rad_req, struct radius_msg ** rad_ans, struct msg ** diam_fw, struct rgw_client * cli )
+static int sip_rad_req( struct rgwp_config * cs, struct radius_msg * rad_req, struct radius_msg ** rad_ans, struct msg ** diam_fw, struct rgw_client * cli )
 {
 	int idx;
@@ -340,19 +341,18 @@
 	struct avp *auth_data=NULL, *auth=NULL, *avp = NULL;
 	union avp_value value;
-	
-	TRACE_ENTRY("%p %p %p %p %p %p", cs, session, rad_req, rad_ans, diam_fw, cli);
-	
-	CHECK_PARAMS(rad_req && (rad_req->hdr->code == RADIUS_CODE_ACCESS_REQUEST) && rad_ans && diam_fw && *diam_fw && session);
-	
-	//We check that session is not already filled
-	if(*session)
-	{
-		TRACE_DEBUG(INFO,"INTERNAL ERROR: We are not supposed to receive a session in radSIP plugin.");
-		return EINVAL;
-	}
+	struct session * sess;
+	
+	TRACE_ENTRY("%p %p %p %p %p", cs, rad_req, rad_ans, diam_fw, cli);
+	
+	CHECK_PARAMS(rad_req && (rad_req->hdr->code == RADIUS_CODE_ACCESS_REQUEST) && rad_ans && diam_fw && *diam_fw);
 	
 	/*
 	   RFC5090 RADIUS Extension Digest Application
 	*/
+	CHECK_FCT( fd_msg_sess_get(fd_g_config->cnf_dict, *diam_fw, &sess, NULL) );
+	if (sess != NULL) {
+		TRACE_DEBUG(INFO,"INTERNAL ERROR: We are not supposed to receive a session in radSIP plugin.");
+		return EINVAL;		
+	}
 	
 	/* Check basic information is there */
@@ -398,5 +398,5 @@
 					return EINVAL;
 				}
-				CHECK_FCT(fd_sess_fromsid (sid, sidlen, session, NULL));
+				CHECK_FCT(fd_sess_fromsid_msg (sid, sidlen, &sess, NULL));
 				free(sid);
 							
@@ -424,5 +424,5 @@
 
 	/* Create the session if it is not already done */
-	if (!*session) {
+	if (!sess) {
 		
 		DiamId_t fqdn;
@@ -431,7 +431,4 @@
 		size_t realm_len;
 		
-		
-		
-		
 		/* Get information on the RADIUS client */
 		CHECK_FCT( rgw_clients_get_origin(cli, &fqdn, &fqdn_len, &realm, &realm_len) );
@@ -440,7 +437,19 @@
 		CHECK_MALLOC( sid = malloc(un_len + 1 /* ';' */ + fd_g_config->cnf_diamid_len + 1 /* '\0' */) );
 		sidlen = sprintf((char *)sid, "%.*s;%s", (int)un_len, un, fd_g_config->cnf_diamid);
-		CHECK_FCT( fd_sess_new(session, fqdn, fqdn_len, sid, sidlen) );
+		CHECK_FCT( fd_sess_new(&sess, fqdn, fqdn_len, sid, sidlen) );
 		free(sid);
 	}
+	
+	/* Now, add the Session-Id AVP at beginning of Diameter message */
+	CHECK_FCT( fd_msg_avp_new ( cs->dict.Session_Id, 0, &avp ) );
+	value.os.data = sid;
+	value.os.len = sidlen;
+	CHECK_FCT( fd_msg_avp_setvalue ( avp, &value ) );
+	CHECK_FCT( fd_msg_avp_add ( *diam_fw, MSG_BRW_FIRST_CHILD, avp) );
+	
+	TRACE_DEBUG(FULL, "[sip.rgwx] Translating new message for session '%s'...", sid);
+	
+	/* Now add this session in the message */
+	CHECK_FCT( fd_msg_sess_set(*diam_fw, sess) );
 		
 	/* Add the Destination-Realm AVP */
@@ -467,18 +476,5 @@
 	
 	CHECK_FCT( fd_msg_avp_setvalue ( avp, &value ) );
-	CHECK_FCT( fd_msg_avp_add ( *diam_fw, MSG_BRW_FIRST_CHILD, avp) );
-	
-	/* Now, add the Session-Id AVP at beginning of Diameter message */
-	CHECK_FCT( fd_sess_getsid(*session, &sid, &sidlen) );
-	
-	TRACE_DEBUG(FULL, "[sip.rgwx] Translating new message for session '%s'...", sid);
-	
-	/* Add the Session-Id AVP as first AVP */
-	CHECK_FCT( fd_msg_avp_new ( cs->dict.Session_Id, 0, &avp ) );
-	value.os.data = sid;
-	value.os.len = sidlen;
-	CHECK_FCT( fd_msg_avp_setvalue ( avp, &value ) );
-	CHECK_FCT( fd_msg_avp_add ( *diam_fw, MSG_BRW_FIRST_CHILD, avp) );
-	CHECK_FCT( fd_msg_sess_set( *diam_fw, *session) );
+	CHECK_FCT( fd_msg_avp_add ( *diam_fw, MSG_BRW_LAST_CHILD, avp) );
 	
 	/*
@@ -721,11 +717,11 @@
 	//fd_msg_dump_walk(1,*diam_fw);
 	
-	/* Store the request identifier in the session (if provided) */
-	if (*session) {
+	/* Store the request identifier in the session */
+	{
 		unsigned char * req_sip;
 		CHECK_MALLOC(req_sip = malloc(16));
 		memcpy(req_sip, &rad_req->hdr->authenticator[0], 16);
 		
-		CHECK_FCT( fd_sess_state_store( cs->sess_hdl, *session, &req_sip ) );
+		CHECK_FCT( fd_sess_state_store( cs->sess_hdl, sess, &req_sip ) );
 	}
 	
@@ -734,18 +730,18 @@
 }
 
-static int sip_diam_ans( struct rgwp_config * cs, struct session * session, struct msg ** diam_ans, struct radius_msg ** rad_fw, struct rgw_client * cli, int * statefull )
+static int sip_diam_ans( struct rgwp_config * cs, struct msg ** diam_ans, struct radius_msg ** rad_fw, struct rgw_client * cli )
 {
 	
 	
-	struct avp *avp, *next, *asid;
-	struct avp_hdr *ahdr, *sid;
+	struct avp *avp, *next;
+	struct avp_hdr *ahdr;
 	//char buf[254]; /* to store some attributes values (with final '\0') */
 	unsigned char * req_sip = NULL;
-	
-	TRACE_ENTRY("%p %p %p %p %p", cs, session, diam_ans, rad_fw, cli);
-	CHECK_PARAMS(cs && session && diam_ans && *diam_ans && rad_fw && *rad_fw);
-	
-	
-	
+	struct session * sess;
+	os0_t sid = NULL;
+	size_t sidlen;
+	
+	TRACE_ENTRY("%p %p %p %p", cs, diam_ans, rad_fw, cli);
+	CHECK_PARAMS(cs && diam_ans && *diam_ans && rad_fw && *rad_fw);
 	
 	
@@ -777,8 +773,10 @@
 	}
 
-
 	/* Search the different AVPs we handle here */
-	CHECK_FCT( fd_msg_search_avp (*diam_ans, cs->dict.Session_Id, &asid) );
-	CHECK_FCT( fd_msg_avp_hdr ( asid, &sid ) );
+	CHECK_FCT( fd_msg_sess_get(fd_g_config->cnf_dict, *diam_ans, &sess, NULL) );
+	if (sess) {
+		CHECK_FCT( fd_sess_getsid(sess, &sid, &sidlen) );
+	}
+
 
 	/* Check the Diameter error code */
@@ -804,6 +802,5 @@
 			(*rad_fw)->hdr->code = RADIUS_CODE_ACCESS_REJECT;
 			fd_log_debug("[sip.rgwx] Received Diameter answer with error code '%d', session %.*s, translating into Access-Reject",
-					ahdr->avp_value->u32, 
-					sid->avp_value->os.len, sid->avp_value->os.data);
+					ahdr->avp_value->u32, sidlen, sid);
 			return 0;
 	}
@@ -827,12 +824,5 @@
 				case DIAM_ATTR_DIGEST_NONCE:
 					CONV2RAD_STR(DIAM_ATTR_DIGEST_NONCE, ahdr->avp_value->os.data, ahdr->avp_value->os.len, 0);
-					/* Retrieve the request identified which was stored in the session */
-					if (session) {
-						os0_t sid=NULL;
-						size_t sidlen;
-						fd_sess_getsid (session, &sid, &sidlen );
-						
-						nonce_add_element(ahdr->avp_value->os.data, ahdr->avp_value->os.len, sid, sidlen, cs);
-					}
+					nonce_add_element(ahdr->avp_value->os.data, ahdr->avp_value->os.len, sid, sidlen, cs);
 					break;
 				case DIAM_ATTR_DIGEST_REALM:
@@ -867,10 +857,6 @@
 	}
 	
-	if (session) 
-	{
-		CHECK_FCT( fd_sess_state_retrieve( cs->sess_hdl, session, &req_sip ) );
-	}
+	CHECK_FCT( fd_sess_state_retrieve( cs->sess_hdl, sess, &req_sip ) );
 	free(req_sip);
-	
 	
 	return 0;
