EXPORT_C int active_sock_open(lua_State* L)
	{
	RSocketServ sockServ;

	if (sockServ.Connect() != KErrNone)
		{
		return 0;
		}

	/* Register socket server with global context */
	lua_pushstring(L, reinterpret_cast<const char*>(KSocketServ().Ptr()));
	lua_pushnumber(L, sockServ.Handle());
	lua_settable(L, LUA_REGISTRYINDEX);

	luaL_register(L, "socket", active_socket);

	luaL_newmetatable(L, reinterpret_cast<const char*>(KSocketType().Ptr()));
	lua_pushcfunction(L, socket_gc);
	lua_setfield(L, -2, "__gc");
	
	lua_pushstring(L, reinterpret_cast<const char*>(KSocketType().Ptr()));
	lua_settable(L, LUA_REGISTRYINDEX);

	return 1;
	}
/**
 * CSymbianTransportAgent::ConstructL()
 * Second phase construction.
 */
void CSymbianTransportAgent::ConstructL(URL& /* aUrl */)
{
    
    // Open RHTTPSession with default protocol ("HTTP/TCP")
    // Note that RHTTPSession (and consequently the whole 
    // of HTTP) depends on the active scheduler; a scheduler 
    // must be installed when the session is opened and it 
    // must be running if a transaction is actually to do anything. 
    
    // Get the connection manager instance
    FConnection* connection = FConnection::getInstance();
    if (!connection) {
        LOG.error("TransportAgent: no active connection; exiting");
        setError(ERR_HTTP, "No active connection");
        return;
    }
    
    // Session is owned by FConnection!
    RSocketServ* socketServ = connection->getSession();
    RConnection* rConnection = connection->getConnection();
    // reuse active connection, please see:
    // http://wiki.forum.nokia.com/index.php/CS000825_-_Using_an_already_active_connection
    TInt err;
    TRAP( err, iHttpSession.OpenL() );
    User::LeaveIfError( err );

    // Set the session's connection info...
    RStringPool strPool = iHttpSession.StringPool();
    RHTTPConnectionInfo connInfo = iHttpSession.ConnectionInfo();
    // ...to use the socket server
    connInfo.SetPropertyL ( strPool.StringF(HTTP::EHttpSocketServ,RHTTPSession::GetTable() ),
                            THTTPHdrVal (socketServ->Handle()) );
    // ...to use the connection
    connInfo.SetPropertyL ( strPool.StringF(HTTP::EHttpSocketConnection,RHTTPSession::GetTable() ),
                            THTTPHdrVal (REINTERPRET_CAST(TInt, rConnection)) );
     
    // Create the nested active scheduler
    // please see » Symbian OS v9.1 » Symbian OS reference » C++ component
    // reference » Base E32 » CActiveSchedulerWait
    iASWait = new (ELeave) CActiveSchedulerWait();
}
void CHttpController::ConstructL( RConnection& aConnection, RSocketServ& aSocketServ )
	{
	iPersistentHeaders = CHttpHeaders::NewL();
	iSession.OpenL();
	RHTTPConnectionInfo connectionInfo = iSession.ConnectionInfo();
	RStringPool pool = iSession.StringPool();

	connectionInfo.SetPropertyL( pool.StringF( HTTP::EHttpSocketServ, RHTTPSession::GetTable() ),
			THTTPHdrVal( aSocketServ.Handle() ) );

	TInt connectionPtr = REINTERPRET_CAST(TInt, &aConnection);
	connectionInfo.SetPropertyL( pool.StringF( HTTP::EHttpSocketConnection,
			RHTTPSession::GetTable() ), THTTPHdrVal( connectionPtr ) );
	iSession.FilterCollection().RemoveFilter( iSession.StringPool().StringF( HTTP::ERedirect,
			RHTTPSession::GetTable() ) );

	iTimeoutTimer = CPeriodic::NewL( CActive::EPriorityIdle );

	if ( !iOutputEncoder )
		{
		//default content encoder
		iOutputEncoder = CMultipartEncoder::NewL();
		}
	}
void CTestStepBtRomConfigUser002::
TestBluetoothSocketConstructorL(TInt aTestSubCase, TInt& aExpectedError)
	{
		
	// must set the expected error BEFORE the test calls a leaving function
	CBluetoothSocket* btsocket(0);
	switch(aTestSubCase)
		{
	case 0:
		{
		// Even if BT is missing from the ROM, this test will succeed.
		// This is because we are only passing a hint to the socket server,
		// not actually instantiating a Bluetooth connection
		aExpectedError = KErrNone; 
		
		RSocketServ socketServ;
		TSessionPref pref;
		
		// ToDo Check that these are correct
		pref.iAddrFamily = KSockStream;
		pref.iProtocol = KRFCOMM;

		User::LeaveIfError(socketServ.Connect(pref));
		if (socketServ.Handle())
			{
			socketServ.Close();
			}
		}
		break;

	case 1:
		aExpectedError = iBtExcluded ? KErrBadName : KErrNone;

		btsocket = CBluetoothSocket::NewL(*this, iSocketServ, 
										  KSockStream, KRFCOMM);
		delete btsocket;
		break;

	case 2:
		aExpectedError = iBtExcluded ? KErrBadName : KErrNone;

		btsocket = CBluetoothSocket::NewL(*this, iSocketServ, 
										  KSockSeqPacket, KL2CAP);
		delete btsocket;
		break;
	
	case 3:
		aExpectedError = iBtExcluded ? KErrBadName : KErrNotSupported;

		btsocket = CBluetoothSocket::NewL(*this, iSocketServ, 
										  KSockStream, KSDP);
		delete btsocket;
		break;	

	case 4:
		aExpectedError = iBtExcluded ? KErrBadName : KErrNone;

		btsocket = CBluetoothSocket::NewL(*this, iSocketServ, 
										  KUndefinedSockType, KBTLinkManager);
		delete btsocket;
		break;
		
	case 5:
		aExpectedError = iBtExcluded ? KErrBadName : KErrNone;

		btsocket = CBluetoothSocket::NewL(*this, iSocketServ, 
										  KSockDatagram, KAVCTP);
		delete btsocket;
		break;					
		
	case 6:
		aExpectedError = iBtExcluded ? KErrBadName : KErrNone;

		btsocket = CBluetoothSocket::NewL(*this, iSocketServ, 
										  KSockDatagram, KAVDTP);
		delete btsocket;
		break;						
			
	case 7:
		aExpectedError = iBtExcluded ? KErrBadName : KErrNone;

		btsocket = CBluetoothSocket::NewLC(*this, iSocketServ, 
										  KSockStream, KRFCOMM);
		CleanupStack::PopAndDestroy(btsocket);
		break;

	case 8:
		aExpectedError = iBtExcluded ? KErrBadName : KErrNone;

		btsocket = CBluetoothSocket::NewLC(*this, iSocketServ, 
										  KSockSeqPacket, KL2CAP);
		CleanupStack::PopAndDestroy(btsocket);
		break;
	
	case 9:
		aExpectedError = iBtExcluded ? KErrBadName : KErrNotSupported;

		btsocket = CBluetoothSocket::NewLC(*this, iSocketServ, 
										  KSockStream, KSDP);
		CleanupStack::PopAndDestroy(btsocket);
		break;	

	case 10:
		aExpectedError = iBtExcluded ? KErrBadName : KErrNone;

		btsocket = CBluetoothSocket::NewLC(*this, iSocketServ, 
										  KUndefinedSockType, KBTLinkManager);
		CleanupStack::PopAndDestroy(btsocket);
		break;
		
	case 11:
		aExpectedError = iBtExcluded ? KErrBadName : KErrNone;

		btsocket = CBluetoothSocket::NewLC(*this, iSocketServ, 
										  KSockDatagram, KAVCTP);
		CleanupStack::PopAndDestroy(btsocket);
		break;					
		
	case 12:
		aExpectedError = iBtExcluded ? KErrBadName : KErrNone;

		btsocket = CBluetoothSocket::NewLC(*this, iSocketServ, 
										  KSockDatagram, KAVDTP);
		CleanupStack::PopAndDestroy(btsocket);
		break;	
		
	case 13: // use the protocol descriptor from bt_sock
		aExpectedError = iBtExcluded ? KErrNotFound : KErrNone;

		btsocket = CBluetoothSocket::NewL(*this, iSocketServ, KRFCOMMDesC);
		delete btsocket;
		break;		

	case 14: // use the protocol descriptor from bt_sock
		aExpectedError = iBtExcluded ? KErrNotFound : KErrNone;

		btsocket = CBluetoothSocket::NewL(*this, iSocketServ, KL2CAPDesC);
		delete btsocket;
		break;		
		
	case 15:
		aExpectedError = iBtExcluded ? KErrNotFound : KErrNotSupported;

		btsocket = CBluetoothSocket::NewL(*this, iSocketServ, KProtocolSdp);
		delete btsocket;
		break;				

	case 16:
		aExpectedError = iBtExcluded ? KErrNotFound : KErrNone;

		btsocket = CBluetoothSocket::NewL(*this, iSocketServ, 
										  KProtocolBtLinkManager);
		delete btsocket;
		break;				

	case 17:
		aExpectedError = iBtExcluded ? KErrNotFound : KErrNone;

		btsocket = CBluetoothSocket::NewL(*this, iSocketServ, 
										  KAVDTPProtocolName);
		delete btsocket;
		break;				

	case 18: // use the protocol descriptor from bt_sock
		aExpectedError = iBtExcluded ? KErrNotFound : KErrNone;

		btsocket = CBluetoothSocket::NewLC(*this, iSocketServ, KRFCOMMDesC);
		CleanupStack::PopAndDestroy(btsocket);
		break;		

	case 19: // use the protocol descriptor from bt_sock
		aExpectedError = iBtExcluded ? KErrNotFound : KErrNone;

		btsocket = CBluetoothSocket::NewLC(*this, iSocketServ, KL2CAPDesC);
		CleanupStack::PopAndDestroy(btsocket);
		break;		
		
	case 20:
		aExpectedError = iBtExcluded ? KErrNotFound : KErrNotSupported;

		btsocket = CBluetoothSocket::NewLC(*this, iSocketServ, KProtocolSdp);
		CleanupStack::PopAndDestroy(btsocket);
		break;				

	case 21:
		aExpectedError = iBtExcluded ? KErrNotFound : KErrNone;

		btsocket = CBluetoothSocket::NewLC(*this, iSocketServ, 
										   KProtocolBtLinkManager);
		CleanupStack::PopAndDestroy(btsocket);
		break;				

	case 22:
		aExpectedError = iBtExcluded ? KErrNotFound : KErrNone;

		btsocket = CBluetoothSocket::NewLC(*this, iSocketServ, 
										   KAVDTPProtocolName);
		CleanupStack::PopAndDestroy(btsocket);
		break;			

	case 23:
		aExpectedError = KErrNone; // even with no BT, this will succeed 
		btsocket = CBluetoothSocket::NewL(*this, iSocketServ);
		// Even though we can create a blank (CBluetooth)Socket, this isn't
		// of any use without an instantiated Bluetooth socket, so it doesn't
		// make sense to do any further API calls with this
		delete btsocket;
		break;

	case 24:
		aExpectedError = KErrNone; // even with no BT, this will succeed
		btsocket = CBluetoothSocket::NewLC(*this, iSocketServ);
		// Even though we can create a blank (CBluetooth)Socket, this isn't
		// of any use without an instantiated Bluetooth socket, so it doesn't
		// make sense to do any further API calls with this
		CleanupStack::PopAndDestroy(btsocket);
		break;		

	/*
	CBluetoothSocket* NewL and NewLC taking an RConnection does not
	have to be tested, the RConnection cannot be started over
	Bluetooth BT_ROMCONFIG_PAN_001 confirms that this is not possible
	without BT on ROM
	*/	
	
	/* CBluetoothSocket* NewL and NewLC taking parameters
	(MBluetoothSocketNotifier &aNotifier, RSocketServ &aServer,
	RSocket &aSocket); does not have to be tested, as the API states:
	"This should be used where an existing API returned a RSocket
	representing a Bluetooth connection."  BT_ROMCONFIG_RSOCKET_001
	confirms that this is not possible without BT on ROM
	*/	

	default:
		User::Panic(KInvalidTestPanicString, aTestSubCase);
		break;
		}
	}