function buyTokens(address _beneficiary) public payable {

        uint256 weiAmount = msg.value;
        _preValidatePurchase(_beneficiary, weiAmount);

        uint256 nowTime = block.timestamp;
        
        while (currentPhase < phases.length && phases[currentPhase].till < nowTime) {
            currentPhase = currentPhase.add(1);
        }

        
        if (currentPhase == 0) {
            require(weiAmount >= 1 ether);
        }

        
        uint256 tokens = _getTokenAmount(weiAmount);
        
        TokenLockType lockType = _getTokenLockType(weiAmount);

        if (lockType != TokenLockType.TYPE_NOT_LOCK) {
            require(saleCount[_beneficiary].add(1) <= MAX_SALECOUNT_PER_ADDRESS);
            saleCount[_beneficiary] = saleCount[_beneficiary].add(1);
        }

        
        weiRaised = weiRaised.add(weiAmount);

        _deliverTokens(_beneficiary, tokens, lockType);
        emit TokenPurchase(
            msg.sender,
            _beneficiary,
            weiAmount,
            tokens
        );

        _forwardFunds();
    }