private void InitCoreIbetEngine()
        {
            if (_ibetEngine != null && _ibetEngine.ibetAgent.State == AgentState.Running)
            {
                this._ibetEngine.ibetAgent.Logout();
                this.lblIbetCurrentCredit.Caption = "-";
                this.iNew.Enabled = false;
                this.iLoadStrategy.Enabled = false;
            }
            else
            {
                BCCore.BetAccount ibetAccount = new BCCore.BetAccount();
                BCCore.BetAccount ibetAccount2 = new BCCore.BetAccount();
                
                ibetAccount.Account = config.Ibet.Account;
                ibetAccount.Password = config.Ibet.Password;
                ibetAccount.Website = config.Ibet.Website;                

                BCCore.Utis.EngineLogger logger = new BCCore.Utis.EngineLogger("log");
                if (config.Ibet2 != null)
                {
                    ibetAccount2.Account = config.Ibet2.Account;
                    ibetAccount2.Password = config.Ibet2.Password;
                    ibetAccount2.Website = config.Ibet2.Website;
                    this._ibetEngine = new IBetEngine(ibetAccount, logger, "Engine1", 
                                                      ibetAccount2, logger, "Engine2");
                }
                else
                    this._ibetEngine = new IBetEngine(ibetAccount, logger, "Engine1");
                BCCore.LoginStatus loginStatus = this._ibetEngine.ibetAgent.Login();
                //while (loginStatus == BCCore.LoginStatus.InvalidCaptcha)
                //{
                //    this._ibetEngine.ibetAgent.Login();
                //}
                
                if (loginStatus == BCCore.LoginStatus.OK)
                {
                    this.rpgIbet.Text = "IBET - " + ibetAccount.Account;
                    this._ibetEngine.UpdateCompleted += new EngineDelegate(this._ibetEngine_UpdateCompleted);
                    this._ibetEngine.ibetAgent.RefreshCredit();
                    this._ibetEngine.ibetAgent.RefreshBetList();
                    this.lblIbetCurrentCredit.Caption = this._ibetEngine.ibetAgent.Config.Balance.ToString() + " (" + this._ibetEngine.ibetAgent.Config.Cash.ToString() + ")";
                    this.btnStart.Enabled = true;
                    this.iNew.Enabled = true;
                    this.iLoadStrategy.Enabled = true;
                    this.btnIbetGetInfo.Caption = "Log Out";
                    if (config.Ibet2 != null)
                    {
                        this.iNewR2IB.Enabled = true;
                    }
                }
            }
        }
        private TransactionDTO PlaceBet(bool sendOrNOT, MatchDTO ibetMatch, MatchDTO sbobetMatch, eOddType oddType, string ibetOddID, string sbobetOddID, string ibetOddType, string sbobetOddType, string ibetOddValue, string sbobetOddValue, int ibetStake, int sbobetStake, IBetEngine ibetEngine, SbobetEngine sbobetEngine, string sbobetOdd, bool ibetHomeGive)
        {
            this._betting = true;
            TransactionDTO transactionDTO = new TransactionDTO();
            //transactionDTO.OddType = oddType.ToString() + " - " + ibetOddType.Replace("h", "Home").Replace("a", "Away") + " / " + sbobetOddType.Replace("h", "Home").Replace("a", "Away");
            string text = "";
            string text2 = "";
            string text3 = "";
            bool flag = false;
            bool flag2 = false;
            int num = 0;
            int num2 = 0;
            string text4 = "";
            string text5 = "";
            string text6 = "";
            bool flag3 = false;
            bool flag4 = false;
            bool sBOBETReTrade = false;
            int num3 = 0;
            string text7 = "";
            string betCount = "";
            string text8 = "";
            bool flag5 = false;

            float ibetOddValue_ = float.Parse(ibetOddValue);
            float sboOddValue_ = float.Parse(sbobetOddValue);

            if (ibetOddValue_ > 0.84 || (ibetOddValue_ > 0f && txtAddValue.Value.ToString() == "0.01"))
            {
                float valueAdd = ibetStake * (float)txtAddValue.Value;
                ibetStake = ibetStake + (int)valueAdd;
            }
            if (sboOddValue_ > 0.84 || (sboOddValue_ > 0f && txtAddValue.Value.ToString() == "0.01"))
            {
                float valueAdd = sbobetStake * (float)txtAddValue.Value;
                sbobetStake = sbobetStake + (int)valueAdd;
            }

            if (MatchDTO.IsSameMatch(ibetMatch.HomeTeamName.ToLower(), sbobetMatch.HomeTeamName.ToLower(), ibetMatch.AwayTeamName.ToLower(), sbobetMatch.AwayTeamName.ToLower()))
            {
                try
                {
                    ibetEngine.PrepareBet(ibetOddID, ibetOddType, ibetOddValue, ibetStake.ToString(), out flag, out num, out num2, out text4, out text2, out text3);
                    sbobetEngine.PrepareBet(sbobetOddID, sbobetOddValue, sbobetOddType, out betCount, out num3, out flag3, out text7, out text5, out text6);
                    if (MatchDTO.IsSameMatch(text2, text5, text3, text6))
                    {
                        if (flag && flag3)
                        {
                            if ((checkEdit13.Checked || checkEdit15.Checked) && sendOrNOT)//gui lenh
                            {
                                var taskA = new Task(() => this.AddOddToLocalCommunity(ibetMatch, sbobetMatch, oddType, sbobetOdd, sbobetOdd, ibetOddType, sbobetOddType, ibetOddValue, sbobetOddValue, ibetHomeGive));
                                taskA.Start();
                            }
                            float num4 = float.Parse(text4);
                            float num5 = float.Parse(text7);
                            if (num4 + num5 == 0f || num4 == num5)
                            {
                                if (ibetStake <= num2 && sbobetStake <= num3)
                                {
                                    try
                                    {                                        
                                        float currentCredit = 0;
                                        float currentCredit2 = 0;
                                        if (checkEdit8.Checked)
                                        {
                                            currentCredit = ibetEngine.GetCurrentCredit();
                                            currentCredit2 = sbobetEngine.GetCurrentCredit();
                                        }
                                        else
                                        {
                                            currentCredit = ibetEngine._currentCredit;
                                            currentCredit2 = sbobetEngine._currentCredit;
                                        }

                                        if ((float)ibetStake <= currentCredit && (float)sbobetStake <= currentCredit2)
                                        {
                                            try
                                            {
                                                
                                                ibetEngine.ConfirmBet(oddType, ibetOddValue, ibetStake.ToString(), num.ToString(), num2.ToString(), out flag2);
                                                if (flag2)
                                                {
                                                    sbobetEngine.ConfirmBet(sbobetOddID, sbobetOddValue, sbobetOddType, sbobetStake.ToString(), betCount, out text8, out flag4);
                                                    if (!flag4)
                                                    {
                                                        try
                                                        {
                                                            sbobetEngine.PrepareBet(sbobetOddID, text8, sbobetOddType, out betCount, out num3, out flag5, out text7, out text5, out text6);
                                                            if (MatchDTO.IsSameMatch(text2, text5, text3, text6))
                                                            {
                                                                num5 = float.Parse(text7);
                                                                if (num4 + num5 == 0f || num4 == num5)
                                                                {
                                                                    text = "Retrade. IBET: " + ibetOddValue + " -  SBOBET: " + text8;
                                                                    sbobetEngine.ConfirmBet(sbobetOddID, text8, sbobetOddType, sbobetStake.ToString(), betCount, out text8, out sBOBETReTrade);
                                                                    this._lastTransactionTime = System.DateTime.Now;
                                                                }
                                                                else
                                                                {
                                                                    text = "Invalid Odd while Retrade. IBET Odd: " + text4 + " - SBOBET Odd:" + text7;
                                                                }
                                                            }
                                                            else
                                                            {
                                                                text = string.Concat(new string[]
																{
																	"Not Same Match - Retrade. IBET: ", 
																	text2, 
																	" / ", 
																	text3, 
																	" - SBOBET: ", 
																	text5, 
																	" / ", 
																	text6
																});
                                                            }
                                                        }
                                                        catch (System.Exception ex)
                                                        {
                                                            text = "Error while Retrade. Details: " + ex.Message;
                                                        }
                                                    }
                                                    this._lastTransactionTime = System.DateTime.Now;
                                                    object obj = text;
                                                    text = string.Concat(new object[]
													{
														obj, 
														" - Success Transaction. Half: ", 
														ibetMatch.Half, 
														" - Minute: ", 
														ibetMatch.Minute, 
														" - Halftime: ", 
														ibetMatch.IsHalfTime
													});
                                                }
                                                
                                            }
                                            catch (System.Exception ex)
                                            {
                                                text = "Error while Trading. Details: " + ex.Message;
                                            }
                                        }
                                        else
                                        {
                                            text = string.Concat(new object[]
											{
												"Out of Cash. IBET Credit: ", 
												currentCredit, 
												" -  SBOBET Credit: ", 
												currentCredit2
											});
                                        }
                                    }
                                    catch (System.Exception ex)
                                    {
                                        text = "Error while getting Credit. Details: " + ex.Message;
                                    }
                                }
                                else
                                {
                                    text = string.Concat(new object[]
									{
										"Max Bet. IBET: ", 
										num2, 
										" - SBOBET: ", 
										num3
									});
                                }
                            }
                            else
                            {
                                text = "Invalid Odd while Preparing Ticket. IBET Odd: " + text4 + " - SBOBET Odd:" + text7;
                            }
                        }
                    }
                    else
                    {
                        text = string.Concat(new string[]
						{
							"Not Same Match - Preparing Ticket. IBET: ", 
							text2, 
							" / ", 
							text3, 
							" - SBOBET: ", 
							text5, 
							" / ", 
							text6
						});
                    }
                }
                catch (System.Exception ex)
                {
                    text = "Error while Preparing Trade. Details: " + ex.Message;
                }
            }
            else
            {
                text = string.Concat(new string[]
				{
					"Not Same Match - Comparing. IBET: ", 
					ibetMatch.HomeTeamName, 
					" / ", 
					ibetMatch.AwayTeamName, 
					" - SBOBET: ", 
					sbobetMatch.HomeTeamName, 
					" / ", 
					sbobetMatch.AwayTeamName
				});
            }
            transactionDTO.HomeTeamName = ibetMatch.HomeTeamName + " / " + sbobetMatch.HomeTeamName;
            transactionDTO.AwayTeamName = ibetMatch.AwayTeamName + " / " + sbobetMatch.AwayTeamName;
            transactionDTO.Odd = text4 + " / " + text7;
            transactionDTO.OddKindValue = text4 + " / " + text7;
            transactionDTO.OddValue = ibetOddValue + " / " + sbobetOddValue;
            transactionDTO.Stake = ibetStake + " / " + sbobetStake;
            transactionDTO.IBETAllow = flag;
            transactionDTO.IBETTrade = flag2;
            transactionDTO.IBETReTrade = false;
            transactionDTO.SBOBETAllow = flag3;
            transactionDTO.SBOBETTrade = flag4;
            transactionDTO.SBOBETReTrade = sBOBETReTrade;
            transactionDTO.OddType = oddType.ToString();
            transactionDTO.Note = text;
            transactionDTO.DateTime = System.DateTime.Now;
            transactionDTO.OddType = oddType.ToString() + " - " + ibetOddType.Replace("h", "Home").Replace("a", "Away") + " / " + sbobetOddType.Replace("h", "Home").Replace("a", "Away");
            this._betting = false;
            return transactionDTO;
            
        }
        private TransactionDTO PlaceBetAllowMaxBet(bool sendOrNOT, MatchDTO ibetMatch, MatchDTO sbobetMatch, eOddType oddType, string ibetOddID, string sbobetOddID, string ibetOddType, string sbobetOddType, string ibetOddValue, string sbobetOddValue, int ibetStake, int sbobetStake, bool ibetHomeGive, IBetEngine ibetEngine, SbobetEngine sbobetEngine, string sbobetOdd)
        {
            this._betting = true;
            TransactionDTO transactionDTO = new TransactionDTO();
            
            //transactionDTO.OddType = current2.Type.ToString() + " - Home / Away";
            string text = "";
            string text2 = "";
            string text3 = "";
            bool flag = false;
            bool flag2 = false;
            int num = 0;
            int num2 = 0;
            string text4 = "";
            string text5 = "";
            string text6 = "";
            bool flag3 = false;
            bool flag4 = false;
            bool sBOBETReTrade = false;
            int num3 = 0;
            string text7 = "";
            string betCount = "";
            string text8 = "";
            bool flag5 = false;

            float ibetOddValue_ = float.Parse(ibetOddValue);
            float sboOddValue_ = float.Parse(sbobetOddValue);

            if (ibetOddValue_ > 0.84 || (ibetOddValue_ > 0f && txtAddValue.Value.ToString() == "0.01"))
            {
                float valueAdd = ibetStake * (float)txtAddValue.Value;
                ibetStake = ibetStake + (int)valueAdd;
            }
            if (sboOddValue_ > 0.84 || (sboOddValue_ > 0f && txtAddValue.Value.ToString() == "0.01"))
            {
                float valueAdd = sbobetStake * (float)txtAddValue.Value;
                sbobetStake = sbobetStake + (int)valueAdd;
            }

            bool ibetGoFirst = true;// who is Away or Under

            if (checkEdit11.Checked) //safe bet
            {
                if (sbobetOddType == "a") //  ibet  vs sbo
                {
                    if (oddType == eOddType.FirstHalfOverUnder || oddType == eOddType.FulltimeOverUnder)//neu bong over under
                    {
                        ibetGoFirst = false;
                    }
                    else // bong handicap
                    {
                        if (ibetHomeGive) // neu away la doi cua duoi
                        {
                            ibetGoFirst = false;
                        }
                    }
                }
                else //  sbo vs ibet
                {
                    if (oddType == eOddType.FirstHalfOverUnder || oddType == eOddType.FulltimeOverUnder)//neu bong over under
                    {
                        ibetGoFirst = true;
                    }
                    else // bong handicap
                    {
                        if (ibetHomeGive) // neu away la doi cua duoi
                        {
                            ibetGoFirst = true;
                        }
                    }
                }
            }

            if (MatchDTO.IsSameMatch(ibetMatch.HomeTeamName.ToLower(), sbobetMatch.HomeTeamName.ToLower(), ibetMatch.AwayTeamName.ToLower(), sbobetMatch.AwayTeamName.ToLower()))
            {
                try
                {
                    ibetEngine.PrepareBet(ibetOddID, ibetOddType, ibetOddValue, ibetStake.ToString(), out flag, out num, out num2, out text4, out text2, out text3);
                    sbobetEngine.PrepareBet(sbobetOddID, sbobetOddValue, sbobetOddType, out betCount, out num3, out flag3, out text7, out text5, out text6);
                    //object result = this.CallJavascriptFunction("bet", new object[] { 0, ibetMatch.ID.Remove(0, 2), ibetOddID, ibetOddType, ibetOddValue });
                    
                    if (MatchDTO.IsSameMatch(text2, text5, text3, text6)) // ten hom - away giong nhau
                    {                        
                        float num4 = 0f;
                        float num5 = 0f;
                        if (flag && flag3 && float.TryParse(text4, out num4) && float.TryParse(text7, out num5))
                        {
                            if ((checkEdit13.Checked || checkEdit15.Checked) && sendOrNOT)//gui lenh
                            {
                                var taskA = new Task(() => this.AddOddToLocalCommunity(ibetMatch, sbobetMatch, oddType, sbobetOdd, sbobetOdd, ibetOddType, sbobetOddType, ibetOddValue, sbobetOddValue, ibetHomeGive));
                                taskA.Start();
                            }

                            //try parse de tranh hien tuong mat Odd 1 ben
                            //float num4 = float.Parse(text4); 
                            //float num5 = float.Parse(text7);
                            if (num4 + num5 == 0f || num4 == num5)
                            {
                                int num6 = 0;
                                if (num2 >= num3) // num2 maxbet ibet
                                {
                                    num6 = num3; // num3 maxbet sbo
                                }
                                else
                                {
                                    if (num2 <= num3)
                                    {
                                        num6 = num2;
                                    }
                                }
                                if (ibetStake >= num6)
                                {
                                    ibetStake = num6;
                                }
                                if (sbobetStake >= num6)
                                {
                                    sbobetStake = num6;
                                }
                                try
                                {
                                    float currentCredit = 0;
                                    float currentCredit2 = 0;
                                    if (checkEdit8.Checked)
                                    {
                                        currentCredit = ibetEngine.GetCurrentCredit();
                                        currentCredit2 = sbobetEngine.GetCurrentCredit();
                                    }
                                    else
                                    {
                                        currentCredit = ibetEngine._currentCredit;
                                        currentCredit2 = sbobetEngine._currentCredit;
                                    }

                                    if ((float)ibetStake <= currentCredit && (float)sbobetStake <= currentCredit2)
                                    {
                                        try
                                        {
                                            if (ibetGoFirst)
                                            {
                                                
#if TESTMODE
                                                #region IBET_FIRTST_THEN_SBO
                                                if (!checkEdit12.Checked || (checkEdit12.Checked && ibetOddType == "a" && (oddType == eOddType.FirstHalfOverUnder || oddType == eOddType.FulltimeOverUnder)))
                                                    ibetEngine.ConfirmBet(oddType, ibetOddValue, ibetStake.ToString(), num.ToString(), num2.ToString(), out flag2);
                                                if (flag2 && !checkEdit12.Checked)
                                                {
                                                    sbobetEngine.ConfirmBet(sbobetOddID, sbobetOddValue, sbobetOddType, sbobetStake.ToString(), betCount, out text8, out flag4);
                                                    if (!flag4)
                                                    {
                                                        try
                                                        {
                                                            sbobetEngine.PrepareBet(sbobetOddID, text8, sbobetOddType, out betCount, out num3, out flag5, out text7, out text5, out text6);
                                                            if (MatchDTO.IsSameMatch(text2, text5, text3, text6))
                                                            {
                                                                if (!float.TryParse(text7, out num5)) //text 7 = BetKindValue
                                                                {
                                                                    Thread.Sleep(3000);
                                                                    sbobetEngine.PrepareBet(sbobetOddID, text8, sbobetOddType, out betCount, out num3, out flag5, out text7, out text5, out text6);
                                                                    float.TryParse(text7, out num5);
                                                                }

                                                                if (num4 + num5 == 0f || num4 == num5)
                                                                {
                                                                    text = "Retrade. IBET: " + ibetOddValue + " -  SBOBET: " + text8;
                                                                    //text = "No retrade in this version please trade manually.";
                                                                    sbobetEngine.ConfirmBet(sbobetOddID, text8, sbobetOddType, sbobetStake.ToString(), betCount, out text8, out sBOBETReTrade);
                                                                    this._lastTransactionTime = System.DateTime.Now;
                                                                    playSound(false);
                                                                    
                                                                }
                                                                else
                                                                {
                                                                    text = "Invalid Odd in SBOBET. IBET Odd: " + text4 + " - SBOBET Odd:" + text7;
                                                                    playSound(false);
                                                                }
                                                            }
                                                            else
                                                            {
                                                                text = string.Concat(new string[]
															    {
																    "Not Same Match - Retrade. IBET: ", 
																    text2, 
																    " / ", 
																    text3, 
																    " - SBOBET: ", 
																    text5, 
																    " / ", 
																    text6
															    });
                                                            }
                                                        }
                                                        catch (System.Exception ex)
                                                        {
                                                            text = "Error while Retrade. Details: " + ex.Message;
                                                            playSound(false);
                                                        }
                                                    }
                                                    else
                                                    {
                                                        playSound(true);
                                                    }

                                                    this._lastTransactionTime = System.DateTime.Now;
                                                    object obj = text;
                                                    text = string.Concat(new object[]
												    {
													    obj, 
													    " - Success Transaction. Half: ", 
													    ibetMatch.Half, 
													    " - Minute: ", 
													    ibetMatch.Minute, 
													    " - Halftime: ", 
													    ibetMatch.IsHalfTime
												    });
                                                    ibetEngine.GetCurrentCredit();
                                                    sbobetEngine.GetCurrentCredit();

                                                }
                                                
                                                #endregion
#endif
                                            }

                                            else
                                            {
                                                text = "No more SBOBET comes first.";
                                                //#region SBO_BET_FIRST_THEN_IBET

                                                //sbobetEngine.ConfirmBet(sbobetOddID, sbobetOddValue, sbobetOddType, sbobetStake.ToString(), betCount, out text8, out flag4);
                                                //if (flag4)
                                                //{ 
                                                //    ibetEngine.ConfirmBet(oddType, ibetOddValue, ibetStake.ToString(), num.ToString(), num2.ToString(), out flag2);
                                                //    if (!flag2)
                                                //    {
                                                //        text = "Invalid Odd while Retrade.";// IBET Odd: " + text4 + " - SBOBET Odd:" + text7;
                                                //        sbobetEngine.GetCurrentCredit();
                                                //        playSound(false);
                                                //    }
                                                //    else
                                                //    {
                                                //        this._lastTransactionTime = System.DateTime.Now;
                                                //        object obj = text;
                                                //        text = string.Concat(new object[]
                                                //        {
                                                //            obj, 
                                                //            " - Success Transaction. Half: ", 
                                                //            ibetMatch.Half, 
                                                //            " - Minute: ", 
                                                //            ibetMatch.Minute, 
                                                //            " - Halftime: ", 
                                                //            ibetMatch.IsHalfTime
                                                //        });
                                                //        ibetEngine.GetCurrentCredit();
                                                //        sbobetEngine.GetCurrentCredit();
                                                //        playSound(true);
                                                //    }
                                                //}

                                                //#endregion
                                            }

                                        }
                                        catch (System.Exception ex)
                                        {
                                            text = "Error while Trading. Details: " + ex.Message;
                                        }
                                    }
                                    else
                                    {
                                        text = string.Concat(new object[]
										{
											"Out of Cash. IBET Credit: ", 
											currentCredit, 
											" -  SBOBET Credit: ", 
											currentCredit2
										});
                                    }
                                }
                                catch (System.Exception ex)
                                {
                                    text = "Error while getting Credit. Details: " + ex.Message;
                                }
                            }
                            else
                            {
                                text = "Invalid Odd while Preparing Ticket. IBET Odd: " + text4 + " - SBOBET Odd:" + text7;
                            }
                        }
                    }                        
                    else
                    {
                        text = string.Concat(new string[]
						{
							"Not Same Match - Preparing Ticket. IBET: ", 
							text2, 
							" / ", 
							text3, 
							" - SBOBET: ", 
							text5, 
							" / ", 
							text6
						});
                    }                    
                }
                catch (System.Exception ex)
                {
                    text = "Error while Preparing Trade. Details: " + ex.Message;
                }
            }
            else
            {
                text = string.Concat(new string[]
				{
					"Not Same Match - Comparing. IBET: ", 
					ibetMatch.HomeTeamName, 
					" / ", 
					ibetMatch.AwayTeamName, 
					" - SBOBET: ", 
					sbobetMatch.HomeTeamName, 
					" / ", 
					sbobetMatch.AwayTeamName
				});
            }
            transactionDTO.HomeTeamName = ibetMatch.HomeTeamName + ":" + ibetMatch.HomeScore + " / " + sbobetMatch.HomeTeamName;
            transactionDTO.AwayTeamName = ibetMatch.AwayTeamName + ":" + ibetMatch.AwayScore + " / " + sbobetMatch.AwayTeamName;
            transactionDTO.Odd = text4 + " / " + text7;
            transactionDTO.OddKindValue = text4 + " / " + text7;
            transactionDTO.OddValue = ibetOddValue + " / " + sbobetOddValue;
            transactionDTO.Stake = ibetStake + " / " + sbobetStake;
            transactionDTO.IBETAllow = flag;
            transactionDTO.IBETTrade = flag2;
            transactionDTO.IBETReTrade = false;
            transactionDTO.SBOBETAllow = flag3;
            transactionDTO.SBOBETTrade = flag4;
            transactionDTO.SBOBETReTrade = sBOBETReTrade;
            //transactionDTO.OddType = oddType.ToString() + ;
            transactionDTO.OddType = oddType.ToString() + " - " + ibetOddType.Replace("h", "Home").Replace("a", "Away") + " / " + sbobetOddType.Replace("h", "Home").Replace("a", "Away");
            transactionDTO.Note = text;
            transactionDTO.DateTime = System.DateTime.Now.ToLocalTime();

            transactionDTO.HomeTeamSBOBET = sbobetMatch.HomeTeamName;
            transactionDTO.AwayTeamSBOBET = sbobetMatch.AwayTeamName;
            
            this._betting = false;
            return transactionDTO;            
        }        
 private void TransactionProcess(MatchDTO ibetMatch, MatchDTO sbobetMatch, eOddType oddType, string ibetOddID, string sbobetOddID, string ibetOddType, string sbobetOddType, string ibetOddValue, string sbobetOddValue, int ibetStake, int sbobetStake, IBetEngine ibetEngine, SbobetEngine sbobetEngine, string sbobetOdd, bool homeFavor)
 {
     if (!this._betting)
     {
         TransactionDTO transactionDTO = new TransactionDTO();
         transactionDTO = this.PlaceBet(true, ibetMatch, sbobetMatch, oddType, ibetOddID, sbobetOddID, ibetOddType, sbobetOddType, ibetOddValue, sbobetOddValue.ToString(), ibetStake, sbobetStake, this._ibetEngine, this._sbobetEngine, sbobetOdd, homeFavor);
         this.AddTransaction(transactionDTO);
         if (transactionDTO != null && transactionDTO.IBETTrade)
         {
             this.UpdateOddBetHistory(ibetOddID,"");
         }
     }
 }
        private TransactionDTO PlaceSingleIBET(string BetStrategy, string oddPrice, MatchDTO ibetMatch, string oddID, string oddValue, eOddType oddType, string ibetOddType, string stake, string homeScore, string awayScore, IBetEngine ibetEngine, bool followtype, string followref, string accountName)
        {
            this._betting = true;
            TransactionDTO transactionDTO = new TransactionDTO();
            string acctoScan = "";
            List<Bet> bl;
            string RefID = "";
            if (BetStrategy.Contains("Over15_XA"))
            {
                bl = this._ibetEngine.ibetAgent2.betList;
                acctoScan = this._ibetEngine.ibetAgent2.Config.Account;
                string[] array = BetStrategy.Split(new string[] { "Ref ID:" }, System.StringSplitOptions.None);
                RefID = array[1];
                if (!AllowOddBet(RefID + ibetOddType, ibetOddType))
                {
                    transactionDTO.HomeTeamName = ibetMatch.HomeTeamName;
                    transactionDTO.AwayTeamName = ibetMatch.AwayTeamName;
                    transactionDTO.Note = "Under account: " + acctoScan + " just bet on this odd";
                    transactionDTO.DateTime = DateTime.Now;
                    this._betting = false;
                    return transactionDTO;
                }
            }            
            else
            {
                bl = this._ibetEngine.ibetAgent.betList;
                acctoScan = this._ibetEngine.ibetAgent.Config.Account;
            }
            if (BetStrategy.Contains("Warning"))
            {
                transactionDTO.HomeTeamName = ibetMatch.HomeTeamName;
                transactionDTO.AwayTeamName = ibetMatch.AwayTeamName;
                transactionDTO.Note = BetStrategy;
                transactionDTO.DateTime = DateTime.Now;
                this._betting = false;
                return transactionDTO;
            }
            foreach (Bet bet in bl)
            {
                if (bet.Home == ibetMatch.HomeTeamName)
                {                    
                    if (bet.Handicap == decimal.Parse(oddValue))
                    {
                        if (bet.Choice.ToString().ToLower() == ibetOddType)
                        {
                            transactionDTO.HomeTeamName = ibetMatch.HomeTeamName;
                            transactionDTO.AwayTeamName = ibetMatch.AwayTeamName;
                            transactionDTO.Note = acctoScan + " : Bet list contains this odd already";
                            transactionDTO.DateTime = DateTime.Now;
                            this._betting = false;
                            return transactionDTO;
                        }
                    }                    
                }
            }
            if (!AllowOddBet(oddID + ibetOddType,ibetOddType))
            {
                transactionDTO.HomeTeamName = ibetMatch.HomeTeamName;
                transactionDTO.AwayTeamName = ibetMatch.AwayTeamName;
                transactionDTO.Note = acctoScan + " just bet on this odd";
                transactionDTO.DateTime = DateTime.Now;
                this._betting = false;
                return transactionDTO;
            }
            if (this._ibetEngine.ibetAgent.Config.Balance < int.Parse(stake))
            {
                transactionDTO.HomeTeamName = ibetMatch.HomeTeamName;
                transactionDTO.AwayTeamName = ibetMatch.AwayTeamName;
                transactionDTO.Note = "Not enough money";
                transactionDTO.DateTime = DateTime.Now;
                this._betting = false;
                return transactionDTO;
            }

            
            string following = string.Empty;
            if (followtype)
                following = "following";
            else
                following = "unfollowing";

            string betKindValue = string.Empty;
            string homeTeamName = string.Empty;
            string awayTeamName = string.Empty;
            string newOddValue = string.Empty;
            string newHomeScore = string.Empty;
            string newAwayScore = string.Empty;

            bool flag = false;
            bool flag2 = false;
            string text = string.Empty;

            //System.Collections.Generic.List<MatchDTO> listIbetMatch = this._listIBETMatch;
            Dictionary<string, IbetMatch> listIbetMatch = this._ibetMatchs;
            //MatchDTO matchDTO = MatchDTO.SearchMatchFull(ibetMatch, listIbetMatch);
            if (ibetMatch != null && ibetMatch.HomeTeamName != string.Empty)
            {
                //OddDTO oddDTO = OddDTO.SearchOdd(oddType, odd, true, ibetMatch.Odds);
                //if (oddDTO != null)
                //{
                    string ibetOddNow = string.Empty;
                    //if (ibetOddType == "a")
                    //    ibetOddNow = oddDTO.Away.ToString();
                    //else
                    //    ibetOddNow = oddDTO.Home.ToString();
                    if (followtype || (!followtype && ((float.Parse(ibetOddNow) >= (float)txtLowestOddValue.Value && float.Parse(ibetOddNow) > 0)) || float.Parse(ibetOddNow) < 0))
                    {
                        int maxBet = 0;
                        int minBet = 0;
                        string ibetOddID = oddID;
                        try
                        {
                            string valueAway = "";
                            if (BetStrategy == "Under" || BetStrategy == "Over")
                            {
                                #region UNDER_OVER
                                if (BetStrategy == "Under")
                                {
                                    ibetEngine.PrepareBet2(ibetOddID, "h", oddPrice.ToString(), stake, out flag, out minBet, out maxBet, out betKindValue, out homeTeamName, out awayTeamName, out newOddValue, out newHomeScore, out newAwayScore);
                                    valueAway = newOddValue;
                                    Thread.Sleep(2000);
                                    ibetEngine.PrepareBet2(ibetOddID, "a", oddPrice.ToString(), stake, out flag, out minBet, out maxBet, out betKindValue, out homeTeamName, out awayTeamName, out newOddValue, out newHomeScore, out newAwayScore);
                                }
                                else if (BetStrategy == "Over")
                                {
                                    ibetEngine.PrepareBet2(ibetOddID, "a", oddPrice.ToString(), stake, out flag, out minBet, out maxBet, out betKindValue, out homeTeamName, out awayTeamName, out newOddValue, out newHomeScore, out newAwayScore);
                                    valueAway = newOddValue;
                                    Thread.Sleep(2000);
                                    ibetEngine.PrepareBet2(ibetOddID, "h", oddPrice.ToString(), stake, out flag, out minBet, out maxBet, out betKindValue, out homeTeamName, out awayTeamName, out newOddValue, out newHomeScore, out newAwayScore);
                                }
                                if (homeTeamName == ibetMatch.HomeTeamName)
                                {
                                    if (valueAway == newOddValue)
                                    {
                                        ibetEngine.ConfirmBet(oddType, newOddValue, stake, minBet.ToString(), maxBet.ToString(), out flag2);
                                        if (!flag2)
                                        {
                                            if (BetStrategy == "Under")
                                                text = "Under:: Confirm bet failed";
                                            else
                                                text = "Over:: Confirm bet failed";
                                        }
                                        else
                                        {
                                            this._lastTransactionTime = DateTime.Now;
                                            if (BetStrategy == "Under")
                                            {
                                                text = "Under:: - Success ";
                                                UpdateOddBetHistory(oddID + "a", "a");
                                            }
                                            else
                                            {
                                                text = "Over:: - Success ";
                                                UpdateOddBetHistory(oddID + "h", "h");
                                            }
                                            this._ibetEngine.ibetAgent.RefreshBetList();
                                        }
                                    }
                                    else
                                    {
                                        if (BetStrategy == "Under")
                                            text = "Under:: Odd requested: " + oddPrice.ToString();
                                        else
                                            text = "Over:: Odd requested: " + oddPrice.ToString();
                                    }
                                }
                                else
                                {
                                    if (BetStrategy == "Under")
                                        text = "Under:: Not same match. Comparing: " + ibetMatch.HomeTeamName + " - " + ibetMatch.AwayTeamName;
                                    else
                                        text = "Over:: Not same match. Comparing: " + ibetMatch.HomeTeamName + " - " + ibetMatch.AwayTeamName;
                                }
                                #endregion
                            }
                            else if (BetStrategy == "Over9290")
                            {
                                #region Over9290
                                ibetEngine.PrepareBet2(ibetOddID, "h", oddPrice.ToString(), stake, out flag, out minBet, out maxBet, out betKindValue, out homeTeamName, out awayTeamName, out newOddValue, out newHomeScore, out newAwayScore);
                                if (flag && homeTeamName == ibetMatch.HomeTeamName)
                                {
                                    ibetEngine.ConfirmBet(oddType, newOddValue, stake, minBet.ToString(), maxBet.ToString(), out flag2);
                                    if (!flag2)
                                    {
                                        text = "Over 92/90:: Confirm bet failed";
                                    }
                                    else
                                    {
                                        this._lastTransactionTime = DateTime.Now;
                                        text = "Over 92/90:: - Success ";
                                        //this._ibetEngine.ibetAgent.RefreshBetList();
                                        UpdateOddBetHistory(oddID + "h", "h");
                                    }
                                }
                                else
                                {
                                    text = "Over1.75:: Not same match. Comparing: " + ibetMatch.HomeTeamName + " - " + ibetMatch.AwayTeamName;
                                }
                                #endregion 
                            }
                            else if (BetStrategy == "SapKeo")
                            {
                                #region SAP_KEO
                                stake = ((int)(int.Parse(stake) * 1.5)).ToString();
                                ibetEngine.PrepareBet2(ibetOddID, "h", oddPrice.ToString(), stake, out flag, out minBet, out maxBet, out betKindValue, out homeTeamName, out awayTeamName, out newOddValue, out newHomeScore, out newAwayScore);
                                if (newOddValue == oddPrice.ToString())
                                {
                                    if (homeTeamName == ibetMatch.HomeTeamName)
                                    {
                                        ibetEngine.ConfirmBet(oddType, newOddValue, stake, minBet.ToString(), maxBet.ToString(), out flag2);
                                        if (!flag2)
                                        {
                                            text = "Sap Keo:: Confirm bet failed";
                                        }
                                        else
                                        {
                                            this._lastTransactionTime = DateTime.Now;
                                            text = "Odd Down:: - Success ";
                                            //this._ibetEngine.ibetAgent.RefreshBetList();
                                            UpdateOddBetHistory(oddID + "h", "h");
                                        }
                                    }
                                    else
                                    {
                                        text = "Sap Keo:: Not same match. Comparing: " + ibetMatch.HomeTeamName + " - " + ibetMatch.AwayTeamName;
                                    }
                                }
                                else
                                {
                                    text = "Sap Keo:: Odd change. Requested: " + oddPrice.ToString();
                                }
                                #endregion
                            }
                            else if (BetStrategy == "Over1.75")
                            {
                                #region Over1.75
                                ibetEngine.PrepareBet2(ibetOddID, "h", oddPrice.ToString(), stake, out flag, out minBet, out maxBet, out betKindValue, out homeTeamName, out awayTeamName, out newOddValue, out newHomeScore, out newAwayScore);
                                if (homeTeamName == ibetMatch.HomeTeamName)
                                {
                                    ibetEngine.ConfirmBet(oddType, newOddValue, stake, minBet.ToString(), maxBet.ToString(), out flag2);
                                    if (!flag2)
                                    {
                                        text = "Over1.75:: Confirm bet failed";
                                    }
                                    else
                                    {
                                        this._lastTransactionTime = DateTime.Now;
                                        text = "Over1.75:: - Success ";
                                        //this._ibetEngine.ibetAgent.RefreshBetList();
                                        UpdateOddBetHistory(oddID + "h", "h");
                                    }
                                }
                                else
                                {
                                    text = "Over1.75:: Not same match. Comparing: " + ibetMatch.HomeTeamName + " - " + ibetMatch.AwayTeamName;
                                }
                                #endregion
                            }
                            else if (BetStrategy == "nguoc" || BetStrategy == "xuoi")
                            {
                                #region BEST_STRATEGIES
                                if (BetStrategy == "nguoc")
                                {
                                    ibetEngine.PrepareBet2(ibetOddID, "a", oddPrice.ToString(), stake, out flag, out minBet, out maxBet, out betKindValue, out homeTeamName, out awayTeamName, out newOddValue, out newHomeScore, out newAwayScore);
                                    valueAway = newOddValue;
                                    Thread.Sleep(2000);
                                    ibetEngine.PrepareBet2(ibetOddID, "h", oddPrice.ToString(), stake, out flag, out minBet, out maxBet, out betKindValue, out homeTeamName, out awayTeamName, out newOddValue, out newHomeScore, out newAwayScore);
                                }
                                else if (BetStrategy == "xuoi")
                                {
                                    ibetEngine.PrepareBet2(ibetOddID, "h", oddPrice.ToString(), stake, out flag, out minBet, out maxBet, out betKindValue, out homeTeamName, out awayTeamName, out newOddValue, out newHomeScore, out newAwayScore);
                                    valueAway = newOddValue;
                                    Thread.Sleep(2000);
                                    ibetEngine.PrepareBet2(ibetOddID, "a", oddPrice.ToString(), stake, out flag, out minBet, out maxBet, out betKindValue, out homeTeamName, out awayTeamName, out newOddValue, out newHomeScore, out newAwayScore);
                                }

                                if (homeTeamName == ibetMatch.HomeTeamName)
                                {
                                    if (valueAway == newOddValue)
                                    {
                                        ibetEngine.ConfirmBet(oddType, newOddValue, stake, minBet.ToString(), maxBet.ToString(), out flag2);
                                        if (!flag2)
                                        {
                                            text = "Best Strategy:: Confirm bet failed";
                                        }
                                        else
                                        {
                                            this._lastTransactionTime = DateTime.Now;
                                            text = "Best Strategy:" + this.txtAddValue.Value.ToString() + " : - Success. " + BetStrategy.Replace("nguoc", "Over").Replace("xuoi", "Under");
                                            this._ibetEngine.ibetAgent.RefreshBetList();
                                            if (BetStrategy == "nguoc")
                                            {
                                                UpdateOddBetHistory(oddID + "h", "h");
                                            }
                                            else if (BetStrategy == "xuoi")
                                            {
                                                UpdateOddBetHistory(oddID + "a", "a");
                                            }
                                        }
                                    }
                                    else
                                    {
                                        text = "Best Strategy:: Odd requested: " + oddPrice.ToString();
                                    }
                                }
                                else
                                {
                                    text = "Best Strategy:: Not same match. Comparing: " + ibetMatch.HomeTeamName + " - " + ibetMatch.AwayTeamName;
                                }
                                #endregion
                            }
                            #region IBET_vs_IBET_Over
                            else if (BetStrategy.Contains("Over15_Fang"))
                            {

                                ibetEngine.PrepareBet2(ibetOddID, "h", oddPrice.ToString(), stake, out flag, out minBet, out maxBet, out betKindValue, out homeTeamName, out awayTeamName, out newOddValue, out newHomeScore, out newAwayScore);
                                if (homeTeamName == ibetMatch.HomeTeamName
                                    && (homeTeamName.Contains("30:01-45:00") || homeTeamName.Contains("75:01-90:00"))
                                    )
                                {
                                    ibetEngine.ConfirmBet(oddType, newOddValue, stake, minBet.ToString(), maxBet.ToString(), out flag2);
                                    if (!flag2)
                                    {
                                        text = "Over_15:: Confirm bet failed";
                                    }
                                    else
                                    {
                                        this._lastTransactionTime = DateTime.Now;
                                        text = "Over_15:: - Success ";
                                        UpdateOddBetHistory(oddID + "h", "h");

                                        //this._ibetEngine.ibetAgent.RefreshBetList();
                                    }
                                }
                            }

                            #endregion
                            else if (BetStrategy.Contains("Over15_XA"))
                            {
                                #region IBET_vs_IBET_Over30-45_XA

                                ibetEngine.PrepareBetFor2ndIBET(ibetOddID, ibetOddType, oddPrice.ToString(), stake, out flag, out minBet, out maxBet, out betKindValue, out homeTeamName, out awayTeamName, out newOddValue, out newHomeScore, out newAwayScore);
                                if (homeTeamName.TrimEnd() == ibetMatch.HomeTeamName && awayTeamName.TrimEnd() == ibetMatch.AwayTeamName)
                                {
                                    
                                    if (float.Parse(newOddValue) >= 0.35f || float.Parse(newOddValue) < 0f)//tranh suu ban
                                    {
                                        if (!betKindValue.EndsWith("75"))
                                        {
                                            ibetEngine.ConfirmBetFor2ndIBET(oddType, newOddValue, stake, minBet.ToString(), maxBet.ToString(), out flag2);
                                            if (!flag2)
                                            {
                                                text = BetStrategy + ":: Confirm bet failed";
                                            }
                                            else
                                            {
                                                this._lastTransactionTime = DateTime.Now;
                                                text = BetStrategy + ":: - Success ";
                                                UpdateOddBetHistory(oddID + ibetOddType, ibetOddType);
                                                UpdateOddBetHistory(RefID + ibetOddType, ibetOddType);
                                                this._ibetEngine.ibetAgent2.RefreshBetList();
                                            }
                                        }
                                    }
                                    else
                                    {
                                        text = BetStrategy + ":: Keo nho hon 0.35 >> Hien tai: " + newOddValue + ". Xem xet xa tay >>>>>>>>>>";
                                    }
                                }
                                else
                                {
                                    iBet.Utilities.WriteLog.Write("Not same match: Tim: " + ibetMatch.HomeTeamName + "-" 
                                    + ibetMatch.AwayTeamName + "Tra ve: " + homeTeamName + "-" + awayTeamName);
                                }

                                #endregion


                            }                            
                        }
                        catch (Exception ex)
                        {
                            text = ex.Message;
                        }
                    }
                    else
                    {
                        text = "Odd value is smaller than lowest accepted:" + ibetOddNow;
                    }
                //}
                //else
                //{
                //    string text2 = string.Empty;                    
                //    text = "Odd not found.";
                //}
            }
            else
                text = "Match not found ";


            transactionDTO.HomeTeamName = homeTeamName + ":" + homeScore;
            transactionDTO.AwayTeamName = awayTeamName + ":" + awayScore;
            transactionDTO.HomeScore = homeScore;
            transactionDTO.AwayScore = awayScore;
            transactionDTO.Score = homeScore + "-" + awayScore;
            transactionDTO.AccountPair = this._ibetAccount;// +" - " + this._ibetSubAccount;
            transactionDTO.OddType = oddType.ToString();
            transactionDTO.Odd = betKindValue;
            transactionDTO.OddKindValue = betKindValue;
            transactionDTO.OddValue = newOddValue;
            transactionDTO.Stake = stake;            
            transactionDTO.Note = text + " >>> " + acctoScan;
            transactionDTO.IBETTrade = flag2;
            transactionDTO.IBETAllow = flag;
            transactionDTO.IsFollowTypeTrans = true;
            transactionDTO.DateTime = DateTime.Now;
            transactionDTO.FollowRef = followref;

            this._betting = false;
            return transactionDTO;
        }
		private TransactionDTO PlaceBet(
            MatchDTO ibetMatch,
            MatchDTO threein1betMatch,
            eOddType oddType,
            string ibetOddID,
            string ibetOddType,
            string ibetOddValue,
            OddDTO threein1Odd,
            string threein1OddType,
            int ibetStake,
            int threein1betStake,
            IBetEngine ibetEngine,
            ThreeIn1BetEngine threein1betEngine)
		{
			TransactionDTO transactionDTO = new TransactionDTO();
            string text = string.Empty;
            string text2 = string.Empty;
            string text3 = string.Empty;
			bool flag = false;
			bool flag2 = false;
			int num = 0;
			int num2 = 0;
            string text4 = string.Empty;
            string text5 = string.Empty;
            string text6 = string.Empty;
			bool flag3 = false;
			bool flag4 = false;
			bool sBOBETReTrade = false;
			int num3 = 0;
            
            string text7 = string.Empty;
            string betCount = string.Empty;
            
            string text8 = string.Empty;
			

            bool threein1betAllowance = false;
            int threein1betminStake = 0;
            int threein1betmaxStake = 0;
            string threein1betKindValue = string.Empty;
            string threein1betHomeTeamName = string.Empty;
            string threein1betAwayTeamName = string.Empty;
            string postBODY = string.Empty;  

            if (MatchDTO.IsSameMatch(ibetMatch.HomeTeamName.ToLower(), threein1betMatch.HomeTeamName.ToLower(), ibetMatch.AwayTeamName.ToLower(), threein1betMatch.AwayTeamName.ToLower()))
			{
				try
				{
					ibetEngine.PrepareBet(ibetOddID, ibetOddType, ibetOddValue, ibetStake.ToString(), out flag, out num, out num2, out text4, out text2, out text3);
                    threein1betEngine.PrepareBet(threein1Odd.ID, threein1Odd.Away.ToString(), threein1Odd.Type.ToString().Replace("FulltimeHandicap", "Hdp").Replace("", ""), "Away", threein1Odd.IsHomeGive.ToString().Replace("True", "1").Replace("False", "0"), threein1Odd.Odd, threein1betMatch.HomeScore, threein1betMatch.AwayScore, threein1betMatch.ID, threein1betMatch.IsHalfTime.ToString().Replace("True", "1").Replace("False", "0"), threein1betStake.ToString(),
                        out threein1betAllowance, out threein1betminStake, out threein1betmaxStake, out threein1betKindValue, out threein1betHomeTeamName, out threein1betAwayTeamName, out postBODY);
                    
                    if (MatchDTO.IsSameMatch(text2, text5, text3, text6))
					{
						if (flag && flag3)
						{
							float num4 = float.Parse(text4);
							float num5 = float.Parse(text7);
							if (num4 + num5 == 0f || num4 == num5)
							{
								if (ibetStake <= num2 && threein1betStake <= num3)
								{
									try
									{
										float currentCredit = ibetEngine.GetCurrentCredit();
                                        float currentCredit2 = threein1betEngine.GetCurrentCredit();
										if ((float)ibetStake <= currentCredit && (float)threein1betStake <= currentCredit2)
										{
											try
											{
												ibetEngine.ConfirmBet(oddType, ibetOddValue, ibetStake.ToString(), num.ToString(), num2.ToString(), out flag2);
												if (flag2)
												{
                                                    //threeIn1BetEngine.ConfirmBet(oddType,
                                                    //    threein1betOddValue,
                                                    //    threein1betStake.ToString(), 
                                                    //    betCount, 
                                                    //    text8, 
                                                    //    out flag4);
													if (!flag4)
													{
														try
														{
                                                            //
														}
														catch (System.Exception ex)
														{
															text = "Error while Retrade. Details: " + ex.Message;
														}
													}
													this._lastTransactionTime = System.DateTime.Now;
													object obj = text;
													text = string.Concat(new object[]
													{
														obj, 
														" - Success Transaction. Half: ", 
														ibetMatch.Half, 
														" - Minute: ", 
														ibetMatch.Minute, 
														" - Halftime: ", 
														ibetMatch.IsHalfTime
													});
												}
											}
											catch (System.Exception ex)
											{
												text = "Error while Trading. Details: " + ex.Message;
											}
										}
										else
										{
											text = string.Concat(new object[]
											{
												"Out of Cash. IBET Credit: ", 
												currentCredit, 
												" -  3in1BET Credit: ", 
												currentCredit2
											});
										}
									}
									catch (System.Exception ex)
									{
										text = "Error while getting Credit. Details: " + ex.Message;
									}
								}
								else
								{
									text = string.Concat(new object[]
									{
										"Max Bet. IBET: ", 
										num2, 
										" - 3in1BET: ", 
										num3
									});
								}
							}
							else
							{
								text = "Invalid Odd while Preparing Ticket. IBET Odd: " + text4 + " - 3in1BET Odd:" + text7;
							}
						}
					}
					else
					{
						text = string.Concat(new string[]
						{
							"Not Same Match - Preparing Ticket. IBET: ", 
							text2, 
							" / ", 
							text3, 
							" - 3in1BET: ", 
							text5, 
							" / ", 
							text6
						});
					}
				}
				catch (System.Exception ex)
				{
					text = "Error while Preparing Trade. Details: " + ex.Message;
				}
			}
			else
			{
				text = string.Concat(new string[]
				{
					"Not Same Match - Comparing. IBET: ", 
					ibetMatch.HomeTeamName, 
					" / ", 
					ibetMatch.AwayTeamName, 
					" - 3in1BET: ", 
					threein1betMatch.HomeTeamName, 
					" / ", 
					threein1betMatch.AwayTeamName
				});
			}
            transactionDTO.HomeTeamName = ibetMatch.HomeTeamName + " / " + threein1betMatch.HomeTeamName;
            transactionDTO.AwayTeamName = ibetMatch.AwayTeamName + " / " + threein1betMatch.AwayTeamName;
			transactionDTO.Odd = text4 + " / " + text7;
			transactionDTO.OddKindValue = text4 + " / " + text7;
			transactionDTO.OddValue = ibetOddValue + " / " + threein1Odd.Away;
			transactionDTO.Stake = ibetStake + " / " + threein1betStake;
			transactionDTO.IBETAllow = flag;
			transactionDTO.IBETTrade = flag2;
			transactionDTO.IBETReTrade = false;
			transactionDTO.THREEIN1Allow = flag3;
			transactionDTO.THREEIN1Trade = flag4;
			transactionDTO.THREEIN1ReTrade = sBOBETReTrade;
			transactionDTO.OddType = oddType.ToString();
			transactionDTO.Note = text;
			transactionDTO.DateTime = System.DateTime.Now;
			return transactionDTO;
		}
		private void InitializeIBETEngine()
		{
			if (this._ibetEngine != null)
			{
				this._ibetEngine.Stop();
				System.GC.SuppressFinalize(this._ibetEngine);
			}
			string host = this._ibetDataUrl.Host;
			System.Net.CookieContainer cookieContainer = new System.Net.CookieContainer();
			string[] array = TerminalFormIBET3IN1.GetCookieString(this._ibetDataUrl.AbsoluteUri).Split(new string[]
			{
				";"
			}, System.StringSplitOptions.None);
			string[] array2 = array;
			for (int i = 0; i < array2.Length; i++)
			{
				string text = array2[i];
				string[] array3 = text.Split(new string[]
				{
					"="
				}, System.StringSplitOptions.None);
				if (text.Contains("DispVer"))
				{
					cookieContainer.Add(new System.Uri("http://" + this.webIBET.Url.Host), new System.Net.Cookie("DispVer", "1"));
				}
				else
				{
					cookieContainer.Add(new System.Uri("http://" + this.webIBET.Url.Host), new System.Net.Cookie(array3[0].Trim(), array3[1].Trim()));
				}                                

			}
			string[] array4 = this._ibetDataUrl.Query.Split(new string[]
			{
				"&"
			}, System.StringSplitOptions.None);
			string[] array5 = array4[array4.Length - 1].Split(new string[]
			{
				"="
			}, System.StringSplitOptions.None);
			string dynamicFieldName = array5[0];
			string dynamicFieldValue = array5[1];
			string innerHtml = this.webIBET.Document.Body.Parent.InnerHtml;
			int num = innerHtml.IndexOf("UserName");
			int num2 = innerHtml.IndexOf("imgServerURL");
			string text2 = innerHtml.Substring(num, num2 - num);
			string text3 = text2.Split(new string[]
			{
				";"
			}, System.StringSplitOptions.None)[0].Split(new string[]
			{
				"\""
			}, System.StringSplitOptions.None)[1];
			this.rpgIbet.Text = "IBET - " + text3;
			this._ibetAccount = text3;
			this.Text = string.Concat(new object[]
			{
				this._ibetAccount, 
				" - ", 
				this._3in1Account, 
				" - Version: ", 
				System.Reflection.Assembly.GetExecutingAssembly().GetName().Version
			});
			this._ibetEngine = new IBetEngine(host, text3, dynamicFieldName, dynamicFieldValue, cookieContainer);
			this._ibetEngine.FullDataCompleted += new EngineDelegate(this._ibetEngine_FullDataCompleted);
			this._ibetEngine.UpdateCompleted += new EngineDelegate(this._ibetEngine_UpdateCompleted);            
			this.lblIbetCurrentCredit.Caption = this._ibetEngine.GetCurrentCredit().ToString();
            if (_3in1Engine != null)
            {
                this.btnStart.Enabled = true;
            }
#if DEBUG
            Utilities.WriteLog.Write(".......... IBET engine is started ............");            
#endif        
        }
		private void TerminalForm_FormClosing(object sender, FormClosingEventArgs e)
		{            
			this.Stop();
			if (this._ibetEngine != null)
			{
				this._ibetEngine = null;
			}
			if (this._3in1Engine != null)
			{
                this._3in1Engine = null;
			}
			this._forceRefreshTimer.Stop();
			this.webIBET.Navigate("http://" + this.webIBET.Url.Host + "/logout.aspx");
		}
		private TransactionDTO PlaceBetAllowMaxBet(
            MatchDTO ibetMatch, 
            MatchDTO threein1betMatch, 
            eOddType oddType, 
            string ibetOddID,
            string ibetOddType,             
            string ibetOddValue, 
            OddDTO threein1Odd,
            string threein1OddType,
            int ibetStake, 
            int threein1betStake, 
            IBetEngine ibetEngine, 
            ThreeIn1BetEngine threein1betEngine)
		{
            
			TransactionDTO transactionDTO = new TransactionDTO(); //khoi tao transaction
            string text = string.Empty;			
            bool flagIBETOK = false;
            bool flag3IN1OK = false;
			
			bool threein1ReTrade = false;            
            string betCount = string.Empty;
            
            bool flagibetAllowance = false;
            int ibetminStake = 0;
            int ibetmaxStake = 0;
            string ibetBetKindValue = string.Empty;
            string ibetHomeTeamName = string.Empty;
            string ibetAwayTeamNam = string.Empty;

            bool ibetGoFirst = true;// who is Away or Under
            string threein1OddValue = string.Empty;
            if (threein1OddType == "Away") //neu 3in1 o vi tri away
            {
                threein1OddValue = threein1Odd.Away.ToString();
                if (threein1Odd.Type == eOddType.FirstHalfOverUnder || threein1Odd.Type == eOddType.FulltimeOverUnder) //neu 3in1 la Under
                {
                    ibetGoFirst = false; // cho 3in1 Confirm truoc
                }
                else // bong handicap
                {
                    if (threein1Odd.IsHomeGive) // neu away la doi cua duoi
                    {
                        ibetGoFirst = false;
                    }
                }
            }
            else if (threein1OddType == "Home") // neu 3in1 o vi tri home - ibet away
            {
                threein1OddValue = threein1Odd.Home.ToString();
                if (threein1Odd.Type == eOddType.FirstHalfOverUnder || threein1Odd.Type == eOddType.FulltimeOverUnder) //neu 3in1 la Over
                {
                    ibetGoFirst = true; // cho ibet Confirm truoc (under)
                }
                else // bong handicap
                {
                    if (!threein1Odd.IsHomeGive) // neu 3in1 la doi cua duoi
                    {
                        ibetGoFirst = false;
                    }
                }
            }

            bool flagthreein1betAllowance = false;
            int threein1betminStake = 0;
            int threein1betmaxStake = 0;
            string threein1betKindValue = string.Empty;
            string threein1betHomeTeamName = string.Empty;
            string threein1betAwayTeamName = string.Empty;
            
            string postBODY = string.Empty;            

            if (MatchDTO.IsSameMatch(ibetMatch.HomeTeamName.ToLower(), threein1betMatch.HomeTeamName.ToLower(), ibetMatch.AwayTeamName.ToLower(), threein1betMatch.AwayTeamName.ToLower()))
			{
				try
				{
                    ibetEngine.PrepareBet(ibetOddID, ibetOddType, ibetOddValue, ibetStake.ToString(), 
                        out flagibetAllowance, out ibetminStake, out ibetmaxStake, out ibetBetKindValue, out ibetHomeTeamName, out ibetAwayTeamNam);
                    
                    threein1betEngine.PrepareBet(
                        threein1Odd.ID,
                        threein1OddValue,//odd price
                        threein1Odd.Type.ToString(), 
                        threein1OddType, // "Away"
                        threein1Odd.IsHomeGive.ToString().Replace("True", "1").Replace("False", "0"),
                        threein1Odd.Odd, 
                        threein1betMatch.HomeScore, 
                        threein1betMatch.AwayScore, 
                        threein1betMatch.ID, 
                        threein1betMatch.IsHalfTime.ToString().Replace("True", "1").Replace("False", "0"), 
                        threein1betStake.ToString(),
                        out flagthreein1betAllowance, 
                        out threein1betminStake, 
                        out threein1betmaxStake, 
                        out threein1betKindValue, 
                        out threein1betHomeTeamName, 
                        out threein1betAwayTeamName,
                        out postBODY);
#if DEBUG
                    Utilities.WriteLog.Write("Calling threein1betEngine.PrepareBet");
#endif

                    if (MatchDTO.IsSameMatch(ibetHomeTeamName, threein1betHomeTeamName, ibetAwayTeamNam, threein1betAwayTeamName))
                    {
                        if (flagibetAllowance && flagthreein1betAllowance)
                        {
                            float numIbet = float.Parse(ibetBetKindValue);
                            float num3in1bet = float.Parse(threein1betKindValue);

                            if (numIbet + num3in1bet == 0f || numIbet == num3in1bet)
                            {
#if DEBUG
                                Utilities.WriteLog.Write("+ Valid bet, system is now going to confirm:" + ibetHomeTeamName + " - " + threein1betAwayTeamName);
#endif
                                int numMax = 0; // get minimum of Maximum bet allowed of two system
                                if (ibetmaxStake >= threein1betmaxStake)
                                {
                                    numMax = threein1betmaxStake;                                    
                                }
                                else
                                {
                                    numMax = ibetmaxStake;
                                }

                                if (ibetStake >= numMax) //compare stake with numMax
                                {
                                    ibetStake = numMax;
                                }
                                if (threein1betStake >= numMax)
                                {
                                    threein1betStake = numMax;
                                }

                                try
                                {
                                    float currentCredit = 0;
                                    float currentCredit2 = 0;
                                    if (checkEdit8.Checked)
                                    {
                                        currentCredit = ibetEngine.GetCurrentCredit();
                                        currentCredit2 = threein1betEngine.GetCurrentCredit();
                                    }
                                    else
                                    {
                                        currentCredit = ibetEngine._currentCredit;
                                        currentCredit2 = threein1betEngine._currentCredit;
                                    }
                                    
                                    
                                    if ((float)ibetStake <= currentCredit && (float)threein1betStake <= currentCredit2)
                                    {
                                        if (ibetGoFirst)
                                        {
                                            #region Confirm_IBET_first_then_3in1
#if DEBUG
                                            Utilities.WriteLog.Write("IBET GO FIRST!");
#endif
                                            try
                                            {
                                                ibetEngine.ConfirmBet(oddType, ibetOddValue, ibetStake.ToString(), ibetminStake.ToString(), ibetmaxStake.ToString(), out flagIBETOK);
                                                if (flagIBETOK)// iBet Confirm thanh cong
                                                {
                                                    string odd3in1NEW = string.Empty;
                                                    threein1betEngine.ConfirmBet(postBODY, threein1betStake.ToString(), out flag3IN1OK, out odd3in1NEW);
                                                    if (!flag3IN1OK)
                                                    {
                                                        try
                                                        {
                                                            if (odd3in1NEW != "changed")
                                                            {
                                                                //threein1betEngine.ConfirmBet(postBODY, threein1betStake.ToString(), out flag3IN1OK, out odd3in1NEW);
                                                                this._lastTransactionTime = System.DateTime.Now;
                                                                playSound(false);
                                                                text = "Odd 3in1 changed";
                                                                this.lblIbetCurrentCredit.Caption = this._ibetEngine.GetCurrentCredit().ToString();
                                                                
                                                            }
                                                            else
                                                            {
                                                                text = "Invalid Odd while Retrade.";// IBET Odd: " +text4 + " - SBOBET Odd:" + text7;
                                                                this.lblIbetCurrentCredit.Caption = this._ibetEngine.GetCurrentCredit().ToString();
                                                                playSound(false);
                                                            }
                                                        }
                                                        catch (System.Exception ex)
                                                        {
                                                            text = "Error while Retrade. Details: " + ex.Message;
                                                            playSound(false);

#if DEBUG
                                                            Utilities.WriteLog.Write(text);
#endif
                                                        }
                                                    }
                                                    else
                                                    {
                                                        this.lbl3in1betCurrentCredit.Caption = this._3in1Engine.GetCurrentCredit().ToString();
                                                        this.lblIbetCurrentCredit.Caption = this._ibetEngine.GetCurrentCredit().ToString();
                                                        playSound(true);
                                                    }
                                                    this._lastTransactionTime = System.DateTime.Now;
                                                    object obj = text;
                                                    text = string.Concat(new object[]
												    {
													    obj, 
													    " - Success Transaction. Half: ", 
													    ibetMatch.Half, 
													    " - Minute: ", 
													    ibetMatch.Minute, 
													    " - Halftime: ", 
													    ibetMatch.IsHalfTime
												    });                                                    
                                                }
                                            }
                                            catch (System.Exception ex)
                                            {
                                                text = "Error while Trading. Details: " + ex.Message;
#if DEBUG
                                                Utilities.WriteLog.Write(text);
#endif
                                            }
                                            #endregion
                                        }
                                        else
                                        {
                                            #region Confirm_3in1_first_then_IBET
#if DEBUG
                                            Utilities.WriteLog.Write("3IN1BET GO FIRST!");
#endif
                                            try
                                            {
                                                string odd3in1NEW = string.Empty;
                                                threein1betEngine.ConfirmBet(postBODY, threein1betStake.ToString(), out flag3IN1OK, out odd3in1NEW);
                                                if (flag3IN1OK)
                                                {
                                                    ibetEngine.ConfirmBet(oddType, ibetOddValue, ibetStake.ToString(), ibetminStake.ToString(), ibetmaxStake.ToString(), out flagIBETOK);
                                                    if (!flagIBETOK)// iBet Confirm thanh cong
                                                    {
                                                        try
                                                        {
                                                            playSound(false);
#if DEBUG
                                                            Utilities.WriteLog.Write("iBet confirm failed.");
#endif
                                                        }

                                                        catch (System.Exception ex)
                                                        {
                                                            text = "Error while Retrade. Details: " + ex.Message;
                                                            playSound(false);
#if DEBUG
                                                            Utilities.WriteLog.Write(text);
#endif
                                                        }
                                                    }
                                                    else
                                                    {
                                                        this.lbl3in1betCurrentCredit.Caption = this._3in1Engine.GetCurrentCredit().ToString();
                                                        this.lblIbetCurrentCredit.Caption = this._ibetEngine.GetCurrentCredit().ToString();
                                                        playSound(true);
                                                    }
                                                    this._lastTransactionTime = System.DateTime.Now;
                                                    object obj = text;
                                                    text = string.Concat(new object[]
												    {
													    obj, 
													    " - Success Transaction. Half: ", 
													    ibetMatch.Half, 
													    " - Minute: ", 
													    ibetMatch.Minute, 
													    " - Halftime: ", 
													    ibetMatch.IsHalfTime
												    });
                                                }
                                            }
                                            catch (System.Exception ex)
                                            {
                                                text = "Error while Trading. Details: " + ex.Message;
#if DEBUG
                                                Utilities.WriteLog.Write(text);
#endif
                                            }
                                            #endregion
                                        }
                                    }
                                    else
                                    {
                                        text = string.Concat(new object[]
										{
											"Out of Cash. IBET Credit: ", 
											currentCredit, 
											" -  SBOBET Credit: ", 
											currentCredit2
										});
#if DEBUG
                                        Utilities.WriteLog.Write(text);
#endif
                                    }
                                }
                                catch (System.Exception ex)
                                {
                                    text = "Error while getting Credit. Details: " + ex.Message;
#if DEBUG
                                    Utilities.WriteLog.Write(text);
#endif
                                }



                            }
                            else
                            {
                                text = "Invalid Odd while Preparing Ticket. IBET Odd: " + ibetBetKindValue + " - SBOBET Odd:" + threein1betKindValue;
#if DEBUG
                                Utilities.WriteLog.Write(text);
#endif
                            }
                        }
                        else
                        { 
#if DEBUG
                            if (!flagibetAllowance)
                                Utilities.WriteLog.Write("IBET DIDN'T ALLOW");
                            if (!flagthreein1betAllowance)
                                Utilities.WriteLog.Write("3IN1 DIDN'T ALLOW");
#endif

                        }
                    }

                    else
                    {
                        text = string.Concat(new string[]
						{
							"Not Same Match - Preparing Ticket. IBET: ", 
							ibetHomeTeamName, 
							" / ", 
							ibetAwayTeamNam, 
							" - 3in1BET: ", 
							threein1betHomeTeamName, 
							" / ", 
							threein1betAwayTeamName
						});
#if DEBUG
                        Utilities.WriteLog.Write(text);
#endif
                    }

                        
				}
				catch (System.Exception ex)
				{
					text = "Error while Preparing Trade. Details: " + ex.Message;
#if DEBUG
                    WriteLog.Write(" *** " + text);
#endif 
				}
			}
			else
			{
				text = string.Concat(new string[]
				{
					"Not Same Match - Comparing. IBET: ", 
					ibetMatch.HomeTeamName, 
					" / ", 
					ibetMatch.AwayTeamName, 
					" - 3in1BET: ", 
					threein1betMatch.HomeTeamName, 
					" / ", 
					threein1betMatch.AwayTeamName
				});
			}
            transactionDTO.HomeTeamName = ibetMatch.HomeTeamName + " / " + threein1betMatch.HomeTeamName;
            transactionDTO.AwayTeamName = ibetMatch.AwayTeamName + " / " + threein1betMatch.AwayTeamName;
            transactionDTO.Odd = ibetBetKindValue + " / " + threein1betKindValue;
			transactionDTO.OddKindValue = ibetBetKindValue + " / " + threein1betKindValue;
            transactionDTO.OddValue = ibetOddValue + " / " + threein1OddValue;
			transactionDTO.Stake = ibetStake + " / " + threein1betStake;
			transactionDTO.IBETAllow = flagibetAllowance;
            transactionDTO.IBETTrade = flagIBETOK;
			transactionDTO.IBETReTrade = false;
            transactionDTO.THREEIN1Allow = flagthreein1betAllowance;
            transactionDTO.THREEIN1Trade = flag3IN1OK;
			transactionDTO.THREEIN1ReTrade = threein1ReTrade;
			transactionDTO.OddType = oddType.ToString();
			transactionDTO.Note = text;
			transactionDTO.DateTime = System.DateTime.Now;
			return transactionDTO;
		}
        private TransactionDTO PlaceSingleIBET(MatchDTO ibetMatch, string odd, string oddValue, eOddType oddType, string ibetOddType, string stake, string homeScore, string awayScore, IBetEngine ibetEngine, bool followtype, string followref, string accountName)
        {
            this._betting = true;
            TransactionDTO transactionDTO = new TransactionDTO();
            string following = string.Empty;
            if (followtype)
                following = "following";
            else
                following = "unfollowing";

            string betKindValue = string.Empty;
            string homeTeamName = string.Empty;
            string awayTeamName = string.Empty;
            string newOddValue = string.Empty;
            string newHomeScore = string.Empty;
            string newAwayScore = string.Empty;

            bool flag = false;
            bool flag2 = false;
            string text = string.Empty;

            System.Collections.Generic.List<MatchDTO> listIbetMatch = this._listIBETMatch;
            //MatchDTO matchDTO = MatchDTO.SearchMatchFull(ibetMatch, listIbetMatch);
            if (ibetMatch != null && ibetMatch.HomeTeamName != string.Empty)
            {
                OddDTO oddDTO = OddDTO.SearchOdd(oddType, odd, true, ibetMatch.Odds);
                if (oddDTO != null)
                {
                    string ibetOddNow = string.Empty;
                    if (ibetOddType == "a")
                        ibetOddNow = oddDTO.Away.ToString();
                    else
                        ibetOddNow = oddDTO.Home.ToString();
                    if (followtype || (!followtype && ((float.Parse(ibetOddNow) >= (float)txtLowestOddValue.Value && float.Parse(ibetOddNow) > 0)) || float.Parse(ibetOddNow) < 0))
                    {
                        int maxBet = 0;
                        int minBet = 0;
                        string ibetOddID = oddDTO.ID;
                        try
                        {
                            ibetEngine.PrepareBet2(ibetOddID, ibetOddType, ibetOddNow.ToString(), stake, out flag, out minBet, out maxBet, out betKindValue, out homeTeamName, out awayTeamName, out newOddValue, out newHomeScore, out newAwayScore);
                            if (homeTeamName == ibetMatch.HomeTeamName)
                            {
                                if (newHomeScore == homeScore && newAwayScore == awayScore)
                                {
                                    float num1 = float.Parse(odd);
                                    float num2 = float.Parse(betKindValue);
                                    if (num1 + num2 == 0f || num1 == num2)
                                    {
                                        if (int.Parse(stake) > maxBet)
                                            stake = maxBet.ToString();

                                        float newIbetOddValue = float.Parse(newOddValue);
                                        float oldIbetOddValue = float.Parse(oddValue);

                                        //if (OddDTO.IsValidOddPair(newIbetOddValue, oldIbetOddValue, (float)txtAllowTradeMinValue.Value, true))
                                        //{
                                        ibetEngine.ConfirmBet(oddType, newOddValue, stake, minBet.ToString(), maxBet.ToString(), out flag2);
                                        if (!flag2)
                                        {
                                            //this._lastTransactionTime = System.DateTime.Now;
                                            //SendReportToMainForm("- Follow Bet: " + this._ibetAccount + " >> " + matchDTO.HomeTeamName + " - " + matchDTO.AwayTeamName + ": Bet confirmed failed. ");
                                            text = "Confirm bet failed";
                                        }
                                        else
                                        {
                                            this._lastTransactionTime = DateTime.Now;
                                            text = " - Success " + following + " " + this._ibetSubAccount + ". Follow: " + ibetOddType + " > " + accountName;
                                        }
                                        //}
                                        //else
                                        //{
                                        //    SendReportToMainForm("Follow Bet: " + this._ibetAccount + " >> " + matchDTO.HomeTeamName + " - " + matchDTO.AwayTeamName + ": New odd value: " + newOddValue + " is lower than allowed number ");
                                        //}
                                    }
                                    else
                                        text = "Invalid Odd while Preparing Ticket. IBET Odd: " + betKindValue;
                                }
                                else
                                {
                                    text = "Score changed.";
                                }
                            }
                            else
                            {
                                text = "Not same match. Comparing: " + ibetMatch.HomeTeamName + " - " + ibetMatch.AwayTeamName;
                            }
                        }
                        catch (Exception ex)
                        {
                            text = ex.Message;
                        }
                    }
                    else
                    {
                        text = "Odd value is smaller than lowest accepted:" + ibetOddNow;
                    }
                }
                else
                {
                    string text2 = string.Empty;
                    //foreach (OddDTO o in ibetMatch.Odds)
                    //{
                    //    text2 += o.Odd + ","; 
                    //}
                    text = "Odd not found.";
                }
            }
            else
                text = "Match not found ";


            transactionDTO.HomeTeamName = homeTeamName;
            transactionDTO.AwayTeamName = awayTeamName;
            transactionDTO.HomeScore = homeScore;
            transactionDTO.AwayScore = awayScore;
            transactionDTO.Score = homeScore + "-" + awayScore;
            transactionDTO.AccountPair = this._ibetAccount + " - " + this._ibetSubAccount;
            transactionDTO.OddType = oddType.ToString();
            transactionDTO.Odd = betKindValue;
            transactionDTO.OddKindValue = betKindValue;
            transactionDTO.OddValue = newOddValue;
            transactionDTO.Stake = stake;
            transactionDTO.Note = text;
            transactionDTO.IBETTrade = flag2;
            transactionDTO.IBETAllow = flag;
            transactionDTO.IsFollowTypeTrans = true;
            transactionDTO.DateTime = DateTime.Now;
            transactionDTO.FollowRef = followref;

            this._betting = false;
            return transactionDTO;
        }