bool CActiveStormnode::Register(std::string strService, std::string strKeyStormnode, std::string strTxHash, std::string strOutputIndex, std::string& errorMessage) {
    CTxIn vin;
    CPubKey pubKeyCollateralAddress;
    CKey keyCollateralAddress;
    CPubKey pubKeyStormnode;
    CKey keyStormnode;

    //need correct blocks to send ping
    if(!stormnodeSync.IsBlockchainSynced()) {
        errorMessage = GetStatus();
        LogPrintf("CActiveStormnode::Register() - %s\n", errorMessage);
        return false;
    }

    if(!sandStormSigner.SetKey(strKeyStormnode, errorMessage, keyStormnode, pubKeyStormnode))
    {
        errorMessage = strprintf("Can't find keys for stormnode %s - %s", strService, errorMessage);
        LogPrintf("CActiveStormnode::Register() - %s\n", errorMessage);
        return false;
    }

    if(!GetStormNodeVin(vin, pubKeyCollateralAddress, keyCollateralAddress, strTxHash, strOutputIndex)) {
        errorMessage = strprintf("Could not allocate vin %s:%s for stormnode %s", strTxHash, strOutputIndex, strService);
        LogPrintf("CActiveStormnode::Register() - %s\n", errorMessage);
        return false;
    }

    //TODO(AA): Remove and not limit ports? Or shall we limit ports?
    CService service = CService(strService);
    if(Params().NetworkID() == CChainParams::MAIN) {
        if(service.GetPort() != 31000) {
            errorMessage = strprintf("Invalid port %u for stormnode %s - only 31000 is supported on mainnet.", service.GetPort(), strService);
            LogPrintf("CActiveStormnode::Register() - %s\n", errorMessage);
            return false;
        }
    } else if(service.GetPort() == 31000) {
        errorMessage = strprintf("Invalid port %u for stormnode %s - 31000 is only supported on mainnet.", service.GetPort(), strService);
        LogPrintf("CActiveStormnode::Register() - %s\n", errorMessage);
        return false;
    }

    addrman.Add(CAddress(service), CNetAddr("127.0.0.1"), 2*60*60);

    return Register(vin, CService(strService), keyCollateralAddress, pubKeyCollateralAddress, keyStormnode, pubKeyStormnode, errorMessage);
}
bool CActiveStormnode::Register(std::string strService, std::string strKeyStormnode, std::string txHash, std::string strOutputIndex, std::string& errorMessage) {
	CTxIn vin;
    CPubKey pubKeyCollateralAddress;
    CKey keyCollateralAddress;
    CPubKey pubKeyStormnode;
    CKey keyStormnode;

    if(!sandStormSigner.SetKey(strKeyStormnode, errorMessage, keyStormnode, pubKeyStormnode))
    {
    	LogPrintf("CActiveStormnode::Register() - Error upon calling SetKey: %s\n", errorMessage.c_str());
    	return false;
    }

    if(!GetStormNodeVin(vin, pubKeyCollateralAddress, keyCollateralAddress, txHash, strOutputIndex)) {
		errorMessage = "could not allocate vin";
    	LogPrintf("Register::Register() - Error: %s\n", errorMessage.c_str());
		return false;
	}
	return Register(vin, CService(strService), keyCollateralAddress, pubKeyCollateralAddress, keyStormnode, pubKeyStormnode, errorMessage);
}
bool CActiveStormnode::GetStormNodeVin(CTxIn& vin, CPubKey& pubkey, CKey& secretKey) {
	return GetStormNodeVin(vin, pubkey, secretKey, "", "");
}
//
// Bootup the stormnode, look for a 500 DRKSLK input and register on the network
//
void CActiveStormnode::ManageStatus()
{
    std::string errorMessage;

    if(!fStormNode) return;

    if (fDebug) LogPrintf("CActiveStormnode::ManageStatus() - Begin\n");

    //need correct adjusted time to send ping
    bool fIsInitialDownload = IsInitialBlockDownload();
    if(fIsInitialDownload) {
        status = STORMNODE_SYNC_IN_PROCESS;
        LogPrintf("CActiveStormnode::ManageStatus() - Sync in progress. Must wait until sync is complete to start stormnode.\n");
        return;
    }

    if(status == STORMNODE_INPUT_TOO_NEW || status == STORMNODE_NOT_CAPABLE || status == STORMNODE_SYNC_IN_PROCESS){
        status = STORMNODE_NOT_PROCESSED;
    }

    if(status == STORMNODE_NOT_PROCESSED) {
        if(strStormNodeAddr.empty()) {
            if(!GetLocal(service)) {
                notCapableReason = "Can't detect external address. Please use the stormnodeaddr configuration option.";
                status = STORMNODE_NOT_CAPABLE;
                LogPrintf("CActiveStormnode::ManageStatus() - not capable: %s\n", notCapableReason.c_str());
                return;
            }
        } else {
        	service = CService(strStormNodeAddr);
        }

        LogPrintf("CActiveStormnode::ManageStatus() - Checking inbound connection to '%s'\n", service.ToString().c_str());

          // DRKSLKNOTE: There is no logical reason to restrict this to a specific port.  Its a peer, what difference does it make.
          /*  if(service.GetPort() != 9999) {
                notCapableReason = "Invalid port: " + boost::lexical_cast<string>(service.GetPort()) + " -only 9999 is supported on mainnet.";
                status = STORMNODE_NOT_CAPABLE;
                LogPrintf("CActiveStormnode::ManageStatus() - not capable: %s\n", notCapableReason.c_str());
                return;
            }
        */

        
            if(!ConnectNode((CAddress)service, service.ToString().c_str())){
                notCapableReason = "Could not connect to " + service.ToString();
                status = STORMNODE_NOT_CAPABLE;
                LogPrintf("CActiveStormnode::ManageStatus() - not capable: %s\n", notCapableReason.c_str());
                return;
            }
        

        if(pwalletMain->IsLocked()){
            notCapableReason = "Wallet is locked.";
            status = STORMNODE_NOT_CAPABLE;
            LogPrintf("CActiveStormnode::ManageStatus() - not capable: %s\n", notCapableReason.c_str());
            return;
        }

        // Set defaults
        status = STORMNODE_NOT_CAPABLE;
        notCapableReason = "Unknown. Check debug.log for more information.\n";

        // Choose coins to use
        CPubKey pubKeyCollateralAddress;
        CKey keyCollateralAddress;

        if(GetStormNodeVin(vin, pubKeyCollateralAddress, keyCollateralAddress)) {

            if(GetInputAge(vin) < STORMNODE_MIN_CONFIRMATIONS){
                LogPrintf("CActiveStormnode::ManageStatus() - Input must have least %d confirmations - %d confirmations\n", STORMNODE_MIN_CONFIRMATIONS, GetInputAge(vin));
                status = STORMNODE_INPUT_TOO_NEW;
                return;
            }

            LogPrintf("CActiveStormnode::ManageStatus() - Is capable master node!\n");

            status = STORMNODE_IS_CAPABLE;
            notCapableReason = "";

            pwalletMain->LockCoin(vin.prevout);

            // send to all nodes
            CPubKey pubKeyStormnode;
            CKey keyStormnode;

            if(!sandStormSigner.SetKey(strStormNodePrivKey, errorMessage, keyStormnode, pubKeyStormnode))
            {
            	LogPrintf("Register::ManageStatus() - Error upon calling SetKey: %s\n", errorMessage.c_str());
            	return;
            }

            if(!Register(vin, service, keyCollateralAddress, pubKeyCollateralAddress, keyStormnode, pubKeyStormnode, errorMessage)) {
            	LogPrintf("CActiveStormnode::ManageStatus() - Error on Register: %s\n", errorMessage.c_str());
            }

            return;
        } else {
        	LogPrintf("CActiveStormnode::ManageStatus() - Could not find suitable coins!\n");
        }
    }

    //send to all peers
    if(!Dseep(errorMessage)) {
    	LogPrintf("CActiveStormnode::ManageStatus() - Error on Ping: %s", errorMessage.c_str());
    }
}
//
// Bootup the Stormnode, look for a 10,000 DRKSLK input and register on the network
//
void CActiveStormnode::ManageStatus()
{
    std::string errorMessage;

    if(!fStormNode) return;

    if (fDebug) LogPrintf("CActiveStormnode::ManageStatus() - Begin\n");

    //need correct blocks to send ping
    if(!stormnodeSync.IsBlockchainSynced()) {
        status = ACTIVE_STORMNODE_SYNC_IN_PROCESS;
        LogPrintf("CActiveStormnode::ManageStatus() - %s\n", GetStatus());
        return;
    }

    if(status == ACTIVE_STORMNODE_SYNC_IN_PROCESS) status = ACTIVE_STORMNODE_INITIAL;

    if(status == ACTIVE_STORMNODE_INITIAL) {
        CStormnode *psn;
        psn = snodeman.Find(pubKeyStormnode);
        if(psn != NULL) {
            psn->Check();
            if((psn->IsEnabled() || psn->IsPreEnabled()) && psn->protocolVersion == PROTOCOL_VERSION)
                    EnableHotColdStormNode(psn->vin, psn->addr);
        }
    }

    if(status != ACTIVE_STORMNODE_STARTED) {

        // Set defaults
        status = ACTIVE_STORMNODE_NOT_CAPABLE;
        notCapableReason = "";

        if(pwalletMain->IsLocked()){
            notCapableReason = "Wallet is locked.";
            LogPrintf("CActiveStormnode::ManageStatus() - not capable: %s\n", notCapableReason);
            return;
        }

        if(pwalletMain->GetBalance() == 0){
            notCapableReason = "Hot node, waiting for remote activation.";
            LogPrintf("CActiveStormnode::ManageStatus() - not capable: %s\n", notCapableReason);
            return;
        }

        if(strStormNodeAddr.empty()) {
            if(!GetLocal(service)) {
                notCapableReason = "Can't detect external address. Please use the stormnodeaddr configuration option.";
                LogPrintf("CActiveStormnode::ManageStatus() - not capable: %s\n", notCapableReason);
                return;
            }
        } else {
            service = CService(strStormNodeAddr, true);
        }

        LogPrintf("CActiveStormnode::ManageStatus() - Checking inbound connection to '%s'\n", service.ToString());

        if(Params().NetworkID() == CChainParams::MAIN) {
            if(service.GetPort() != 31000) {
                notCapableReason = strprintf("Invalid port: %u - only 31000 is supported on mainnet.", service.GetPort());
                LogPrintf("CActiveStormnode::ManageStatus() - not capable: %s\n", notCapableReason);
                return;
            }
        } else if(service.GetPort() == 31000) {
            notCapableReason = strprintf("Invalid port: %u - 31000 is only supported on mainnet.", service.GetPort());
            LogPrintf("CActiveStormnode::ManageStatus() - not capable: %s\n", notCapableReason);
            return;
        }

        if(!ConnectNode((CAddress)service, NULL, true)){
            notCapableReason = "Could not connect to " + service.ToString();
            LogPrintf("CActiveStormnode::ManageStatus() - not capable: %s\n", notCapableReason);
            return;
        }

        // Choose coins to use
        CPubKey pubKeyCollateralAddress;
        CKey keyCollateralAddress;

        if(GetStormNodeVin(vin, pubKeyCollateralAddress, keyCollateralAddress)) {

            if(GetInputAge(vin) < STORMNODE_MIN_CONFIRMATIONS){
                status = ACTIVE_STORMNODE_INPUT_TOO_NEW;
                notCapableReason = strprintf("%s - %d confirmations", GetStatus(), GetInputAge(vin));
                LogPrintf("CActiveStormnode::ManageStatus() - %s\n", notCapableReason);
                return;
            }

            LOCK(pwalletMain->cs_wallet);
            pwalletMain->LockCoin(vin.prevout);

            // send to all nodes
            CPubKey pubKeyStormnode;
            CKey keyStormnode;

            if(!sandStormSigner.SetKey(strStormNodePrivKey, errorMessage, keyStormnode, pubKeyStormnode))
            {
                notCapableReason = "Error upon calling SetKey: " + errorMessage;
                LogPrintf("Register::ManageStatus() - %s\n", notCapableReason);
                return;
            }

            if(!Register(vin, service, keyCollateralAddress, pubKeyCollateralAddress, keyStormnode, pubKeyStormnode, errorMessage)) {
                notCapableReason = "Error on Register: " + errorMessage;
                LogPrintf("Register::ManageStatus() - %s\n", notCapableReason);
                return;
            }

            LogPrintf("CActiveStormnode::ManageStatus() - Is capable Stormnode!\n");
            status = ACTIVE_STORMNODE_STARTED;

            return;
        } else {
            notCapableReason = "Could not find suitable coins!";
            LogPrintf("CActiveStormnode::ManageStatus() - %s\n", notCapableReason);
            return;
        }
    }

    //send to all peers
    if(!SendStormnodePing(errorMessage)) {
        LogPrintf("CActiveStormnode::ManageStatus() - Error on Ping: %s\n", errorMessage);
    }
}