void ut_cnatfwsockethandler::ut_cnatfwsockethandler_SetAddrL2L(  )
{
    TSockAddr addr;
    RSocket socket;

    User::LeaveIfError( socket.Open( iSocketServer, KAfInet, KSockDatagram, KProtocolInetUdp ) );
    socket.SetLocalPort( KTestPortUdp );

    // RSocket is stubbed and currently the used ports are not stored
    // so we can make the next localportcall fail with this
    TBool shouldItFail = ETrue;
    socket.NextSetLocalPortShouldFail(ETrue, shouldItFail); // fails

    iHandler->SetMediaObserverL( this );
    iHandler->SetLocalAddress( iSocketServer, iAddr );
    // Will try to use same port as KTestPortUdp, but
    // since it's reserved, should switch to KTestPort2
    NATFW_EUNIT_ASSERT_NO_LEAVE( iHandler->SetAddrL( iAddr ) );

    iHandler->LocalAddress( addr );
    EUNIT_ASSERT_EQUALS( KTestPort2, addr.Port() );
}
enum TVerdict CSocketTest8_4::InternalDoTestStepL( void )
	{
	TVerdict verdict = EPass;
	
	Logger().WriteFormat(_L("Test Purpose: Ioctl Get and Set Options"));
	
	// connect to esock
	Logger().WriteFormat(_L("Attempting to connect to socket server"));
    RSocketServ ss;
	TInt ret = OptimalConnect(ss);
	CleanupClosePushL(ss);
	Logger().WriteFormat(_L("Connect returned %S"), &EpocErrorToText(ret));
	TESTL(KErrNone == ret);
	
	// try SetLocalPort
	Logger().WriteFormat(_L("Opening a socket"));
	RSocket socket;
	ret = socket.Open(ss, _L("Dummy Protocol 1"));
	Logger().WriteFormat(_L("Open returned %S"), &EpocErrorToText(ret));
	TESTL(KErrNone == ret);
	
	Logger().WriteFormat(_L("Trying SetLocalPort to 666"));
	ret = socket.SetLocalPort(666);
	Logger().WriteFormat(_L("SetLocalPort returned %S"), &EpocErrorToText(ret));
	TESTL(KErrNone == ret);
	Logger().WriteFormat(_L("Port now set to %d"), socket.LocalPort());
	TESTL(666 == socket.LocalPort());
	
	// try blocking Get/Set
	Logger().WriteFormat(_L("Testing blocking mode Get and Set options"));
	Logger().WriteFormat(_L("Trying to SetOption KSONonBlockingIO"));
	ret = socket.SetOpt(KSONonBlockingIO, KSOLSocket);
	Logger().WriteFormat(_L("SetOption returned %S"), &EpocErrorToText(ret));
	TESTL(KErrNone == ret);
	
	Logger().WriteFormat(_L("Trying to GetOption KSONonBlockingIO"));
	TPckgBuf<TBool> boolean;
	ret = socket.GetOpt(KSONonBlockingIO, KSOLSocket, boolean);
	Logger().WriteFormat(_L("GetOption returned %S, value %d"), 
		&EpocErrorToText(ret), boolean());
	TESTL(KErrNone == ret);
	TESTL(TRUE == boolean());
	
	Logger().WriteFormat(_L("Trying to GetOption KSOBlockingIO"));
	ret = socket.GetOpt(KSOBlockingIO, KSOLSocket, boolean);
	Logger().WriteFormat(_L("GetOption returned %S, value %d"), 
		&EpocErrorToText(ret), boolean());
	TESTL(KErrNone == ret);
	TESTL(FALSE == boolean());
	
	Logger().WriteFormat(_L("Trying to SetOption KSOBlockingIO"));
	ret = socket.SetOpt(KSOBlockingIO, KSOLSocket);
	Logger().WriteFormat(_L("SetOption returned %S"), &EpocErrorToText(ret));
	TESTL(KErrNone == ret);
	
	Logger().WriteFormat(_L("Trying to GetOption KSONonBlockingIO"));
	ret = socket.GetOpt(KSONonBlockingIO, KSOLSocket, boolean);
	Logger().WriteFormat(_L("GetOption returned %S, value %d"), 
		&EpocErrorToText(ret), boolean());
	TESTL(KErrNone == ret);
	TESTL(FALSE == boolean());
	
	Logger().WriteFormat(_L("Trying to GetOption KSOBlockingIO"));
	ret = socket.GetOpt(KSOBlockingIO, KSOLSocket, boolean);
	Logger().WriteFormat(_L("GetOption returned %S, value %d"), 
		&EpocErrorToText(ret), boolean());
	TESTL(KErrNone == ret);
	TESTL(TRUE == boolean());
	
	CleanupStack::Pop(&ss);
	ss.Close();
	
	return verdict;
	}
TVerdict CmoveConnectedSocketToSubconnectionStep::doSingleTestStep()
	{
	
	RSocket			socket;
	TInetAddr		dstAddr;
	RSocketServ		socketServer;
	RConnection		conn;
	RSubConnection	subconn;
	TRequestStatus	status;
	TRequestStatus 	eventStatus;
	TNotificationEventBuf	subconnNotifBuf;
	TInt				ret;
	
	CleanupClosePushL(socketServer);
   	CleanupClosePushL(conn);
    	CleanupClosePushL(socket);
    	CleanupClosePushL(subconn);

    	// Connect to ESOCK
    	ret = socketServer.Connect();
    	if (ret != KErrNone)
    		{
    		INFO_PRINTF2(_L("Failed to connect toEsock due to Error: %d."),ret);
    		return EFail;
    		}
  
        	// Open a connection
    	ret = conn.Open(socketServer);
	if (ret != KErrNone)
    		{
    		INFO_PRINTF2(_L("Failed to open connection due to Error: %d."),ret);
    		return EFail;
    		}

	// Start the connection
	conn.Start(status);
	User::WaitForRequest(status);
	
	if (status != KErrNone)
    		{
    		INFO_PRINTF2(_L("Failed to start connection due to Error: %d."),status.Int());
    		return EFail;
    		}


	// Open UDP socket
	ret = socket.Open(socketServer,
			KAfInet,
	                      KSockDatagram,
	                      KProtocolInetUdp,
	                      conn);
	if (ret != KErrNone)
    		{
    		INFO_PRINTF2(_L("Failure to open socket due to Error: %d."),ret);
    		return EFail;
    		}
	
	
	dstAddr.SetPort(3441);
	_LIT(KText50, "127.0.0.1");
	dstAddr.Input( KText50 );

	ret = socket.SetLocalPort(3442);
	if (ret != KErrNone)
    		{
    		INFO_PRINTF2(_L("Failed to set port on socket due to Error: %d."),ret);
    		return EFail;
    		}
    		
	socket.Connect(dstAddr, status);
	User::WaitForRequest(status);

	if (status != KErrNone)
    		{
    		INFO_PRINTF2(_L("Socket failed to connect to dst addr due to Error: %d."),status.Int());
    		return EFail;
    		}

	// Create a new sub-connection
	ret = subconn.Open(socketServer,
	                   RSubConnection::ECreateNew,
	                   conn);
	if (ret != KErrNone)
    		{
    		INFO_PRINTF2(_L("Failed to create new sub-connection due to Error: %d."),ret);
    		return EFail;
    		}
    		
	 // Move the connected socket onto the new sub-connection
	subconn.Add(socket, status);
	User::WaitForRequest(status);

	if (status != KErrNotReady)
    		{
    		INFO_PRINTF2(_L("Socket could not be added to subconn due to  Error: %d."),status.Int());
    		return EFail;
    		}

	 // close and destroy
	CleanupStack::PopAndDestroy(&subconn);
	CleanupStack::PopAndDestroy(&socket);
	CleanupStack::PopAndDestroy(&conn);
	CleanupStack::PopAndDestroy(&socketServer);
	
 	return EPass;
	}
enum TVerdict CTestStepNullAgtLoopbackTest::doTestStepL(void)
{
    __UHEAP_MARK;

    TInt r;                // the result of various operations
    TRequestStatus status; // status of asynchronous ops

    RSocketServ server;    // connection paraphanelia
    RConnection connection;
    RSocket socket;

    TInetAddr dest;
    dest.SetAddress(KDummyNifLocalAddressBase + 4);
    dest.SetPort(KPortNo);

    TBuf8<KBufferLength> buffer;

    // connect to the socket server
    r = server.Connect();
    TESTEL(r == KErrNone, r);
    CleanupClosePushL(server);

    // this is why we needed a socket server...
    r = connection.Open(server, KAfInet);
    TESTEL(r == KErrNone, r);
    CleanupClosePushL(connection);

    // start the connection up (outgoing)
    connection.Start(status);
    User::WaitForRequest(status);
    TESTEL(status.Int() == KErrNone, status.Int());

    // open a udp socket
    r = socket.Open(server, KAfInet, KSockDatagram, KProtocolInetUdp);
    TESTEL(r == KErrNone, r);
    CleanupClosePushL(socket);
    TESTL(socket.SetOpt(KSoReuseAddr, KSolInetIp, 1)==KErrNone);
    // set the source port number - otherwise will panic cos it's zero
    r = socket.SetLocalPort(KPortNo);
    TESTEL(r == KErrNone, r);

    // build some data to send on the socket
    // this is an ICMP ping request apparently
    buffer.SetMax();
    buffer.FillZ();
    buffer[0] = (TUint8) 0x8;		// ICMP type = 8
    buffer[1] = (TUint8) 0x0;		// ICMP code = 0
    buffer[2] = (TUint8) 0xF7;		// ICMP checksum high byte
    buffer[3] = (TUint8) 0xFF;		// ICMP checksum low byte
    // NB the rest of the buffer is zero
    // hence the checksum (0xFFFF - 0x800) since 0x8
    // is the only non-zero element of the buffer

    // send the data out over the socket
    socket.SendTo(buffer, dest, 0, status);
    User::WaitForRequest(status);
    TESTEL(status.Int() == KErrNone, status.Int());

    buffer.Zero();
    // I expect to get the data looped back from the dummy NIF
    socket.RecvFrom(buffer, dest, 0, status);
    User::WaitForRequest(status);
    TESTEL(status.Int() == KErrNone, status.Int());

    // check that what we sent is what we got back
    if (status.Int() == KErrNone)
    {
        // if the receive times out and we access buffer we get a panic
        TEST(buffer[0] == 0x08);
        TEST(buffer[1] == 0x00);
        TEST(buffer[2] == 0xF7);
        TEST(buffer[3] == 0xFF);
    }

    // close the socket
    socket.Shutdown(RSocket::ENormal, status);
    User::WaitForRequest(status);
    TESTEL(status.Int() == KErrNone, status.Int());
    CleanupStack::Pop();

    // force the destruction of the connection
    r = connection.Stop();
    TESTEL(r == KErrNone, r);
    CleanupStack::Pop();

    // close the socket server
    server.Close();
    CleanupStack::Pop();

    __UHEAP_MARKEND;

    return iTestStepResult;
}