int ServerMessageProcessor::process_incoming_msg(LSP_Packet& packet)
{
	if(MessageProcessor::process_incoming_msg(packet) == FAILURE)
		return FAILURE;
	switch(packet.getType())
	{
	case CONN_REQ:
	{
		process_conn_req(packet);
		break;
	}
	case ACK:
	{
		process_ack_packet(packet);
		break;
	}
	case DATA:
	{
		process_data_packet(packet);
		break;
	}
	default:
		fprintf( stderr, "Unknown Packet Type!\n");
		packet.print();
	}
	return SUCCESS;
}
sap_internal_error handle_sap_request(int a_remote_sk, sap_msg_id a_event, unsigned char a_param_cnt)
{
        int retval = 0;
        LOGV("->%s: sap_state: %d", __func__, sap_state);

        switch(sap_state) {
                case DISCONNECTED:

                switch(a_event) {
                        case CONNECT_REQ:
                                LOGV("%s: CONNECT_REQ\n", __func__);
                                retval = process_conn_req(a_remote_sk, a_param_cnt);
                                break;
                        case DISCONNECT_REQ:
                                if (error_occured) {
                                        LOGV("%s: DISCONNECT_REQ in disconnected state\n", __func__);
                                        /* honor the disconnect request and send valid
                                         * valid response. Client might send the disconnect request
                                         * as a result of previous error response due to invalid
                                         * connection request */
                                        retval = send_disconn_resp(a_remote_sk);
                                        error_occured = FALSE;
                                }
                                break;
                        default:
                        LOGV("SM Error: State: %d :: Event:%d", sap_state, a_event);
                        retval =  SAP_ERR_ILLEGAL_ARG;
                }break;
                case CONNECTED_READY:
                case DISCONNECTING:
                        if (sap_state == DISCONNECTING){
                                /*TODO*/
                                /*fall through If the state is in Diconnecting
                                elapsed time is leass than the expected*/
                                /*if not, initiate the disconnect immediate*/
                                time_t current_time = time(&current_time);
                                if (difftime(current_time, disconnect_initiated_time) >= DISCONNECT_TIMEOUT) {
                                        alarm(0);
                                        retval = send_disconn_ind(a_remote_sk, IMMEDIATE_DISCONN);
                                        if (retval != SAP_ERR_NONE) {
                                                LOGI("%s: Error while sending the diconn_ind", __func__);
                                        }
                                        qmi_sap_disconnect(UIM_SAP_DISCONNECT_IMMEDIATE_V01, FALSE);

                                        /*Mark this as a QMI error
                                        erro case, so that it cleans up*/
                                        retval = SAP_ERR_QMI;

                                }
                        }
                        switch(a_event) {
                        case TRANSFER_APDU_REQ:
                                LOGV("%s: TRANSFER_APDU_REQ\n", __func__);
                                set_sap_state (PROCESSING_APDU);
                                retval = process_tx_apdu_req(a_remote_sk, a_param_cnt);
                                break;
                        case TRANSFER_ATR_REQ:
                                LOGV("%s: TRANSFER_ATR_REQ\n", __func__);
                                set_sap_state (PROCESSING_ATR);
                                retval = process_tx_atr_req(a_param_cnt);
                                break;
                        case POWER_SIM_OFF_REQ:
                                LOGV("%s: POWER_SIM_OFF_REQ\n", __func__);
                                set_sap_state (PROCESSING_SIM_OFF);
                                retval = process_sim_off_req(a_param_cnt);
                                break;
                        case POWER_SIM_ON_REQ:
                                LOGV("%s: POWER_SIM_ON_REQ\n", __func__);
                                set_sap_state (PROCESSING_SIM_ON);
                                retval = process_sim_on_req(a_param_cnt);
                                break;
                        case RESET_SIM_REQ:
                                LOGV("%s: RESET_SIM_REQ\n", __func__);
                                set_sap_state (PROCESSING_SIM_RESET);
                                retval = process_sim_reset_req(a_param_cnt);
                                break;
                        case TRANSFER_CARD_READER_STATUS_REQ:
                                LOGV("%s: TRANSFER_CARD_READER_STATUS_REQ\n", __func__);
                                set_sap_state (PROCESSING_TX_CARD_RDR_STATUS);
                                retval = process_tx_card_stat_req(a_param_cnt);
                                break;
                        case DISCONNECT_REQ:
                                LOGV("%s: DISCONNECT_REQ\n", __func__);
                                retval = process_disconn_req(a_param_cnt);
                                break;
                        default:
                                LOGV("SM Error: State: %d :: Event:%d", sap_state, a_event);
                                retval =  SAP_ERR_ILLEGAL_ARG;

                        } break;
                case PROCESSING_APDU:
                case PROCESSING_ATR:
                case PROCESSING_SIM_ON:
                case PROCESSING_TX_CARD_RDR_STATUS:
                        {
                                switch(a_event){
                                case DISCONNECT_REQ:
                                        LOGV("%s: DISCONNECT_REQ\n", __func__);
                                        retval = process_disconn_req(a_param_cnt);
                                        break;
                                case POWER_SIM_OFF_REQ:
                                        LOGV("%s: POWER_SIM_OFF_REQ\n", __func__);
                                        set_sap_state (PROCESSING_SIM_OFF);
                                        retval = process_sim_off_req(a_param_cnt);
                                        break;
                                case RESET_SIM_REQ:
                                        LOGV("%s: RESET_SIM_REQ\n", __func__);
                                        set_sap_state (PROCESSING_SIM_RESET);
                                        retval = process_sim_reset_req(a_param_cnt);
                                        break;
                                default:
                                        LOGV("SM Error: State: %d :: Event:%d", sap_state, a_event);
                                        retval =  SAP_ERR_ILLEGAL_ARG;
                                }
                        }break;
                case PROCESSING_SIM_OFF:
                case PROCESSING_SIM_RESET:
                        /*It is not expected to get any request
                        when these are processing*/
                default:
                        LOGV("%s: Default case\n", __func__);
                        retval =  SAP_ERR_ILLEGAL_ARG;
                        break;
        }
        if (retval == SAP_ERR_ILLEGAL_ARG) {
                /*Do a dummy socket read to clear all
                the contents*/
                clear_contents(conn_sk);
                /*Send an ERR_RESP packet to client?*/
                retval = send_error_resp(conn_sk);
                if (SAP_ERR_NONE == retval)
                        /* Set the flag to indicate the error_resp*/
                        error_occured = TRUE;
                       /*Don't need to close the connection,
                        Client may send the Disconnect request
                        after it recieves the error response*/
        }

        LOGV("%s returns %d", __func__, retval);
        return retval;
}