private Object handleResponse(StreamReader streamReader, bool stream, AdharaHFTListener listener)
    {
        try
        {
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            hftResponse          response;
            String line;

            while ((line = streamReader.ReadLine()) != null)
            {
                try{
                    response = serializer.Deserialize <hftResponse>(line);
                }
                catch (Exception)
                {
                    Console.WriteLine("Error reading: " + line);
                    return(null);
                }

                if (response.getAuthorizationChallengeResponse != null)
                {
                    return(response.getAuthorizationChallengeResponse.challenge);
                }

                if (response.getAuthorizationTokenResponse != null)
                {
                    return(response.getAuthorizationTokenResponse.token);
                }

                if (response.getAccountResponse != null)
                {
                    List <accountTick> accountTickList = new List <accountTick>();
                    if (response.getAccountResponse.account != null)
                    {
                        foreach (accountTick tick in response.getAccountResponse.account)
                        {
                            accountTickList.Add(tick);
                        }
                    }
                    return(accountTickList);
                }
                if (response.getInterfaceResponse != null)
                {
                    List <tinterfaceTick> tinterfaceTickList = new List <tinterfaceTick>();
                    if (response.getInterfaceResponse.tinterface != null)
                    {
                        foreach (tinterfaceTick tick in response.getInterfaceResponse.tinterface)
                        {
                            tinterfaceTickList.Add(tick);
                        }
                    }
                    return(tinterfaceTickList);
                }

                if (response.getPriceResponse != null)
                {
                    if (stream)
                    {
                        if (response.getPriceResponse.timestamp != null)
                        {
                            listener.timestampEvent(response.getPriceResponse.timestamp);
                        }
                        if (response.getPriceResponse.tick != null)
                        {
                            listener.priceEvent(response.getPriceResponse.tick);
                        }
                        if (response.getPriceResponse.heartbeat != null)
                        {
                            listener.heartbeatEvent();
                        }
                        if (response.getPriceResponse.message != null)
                        {
                            listener.messageEvent(response.getPriceResponse.message);
                        }
                    }
                    else
                    {
                        List <priceTick> priceTickList = new List <priceTick>();
                        if (response.getPriceResponse.tick != null)
                        {
                            foreach (priceTick tick in response.getPriceResponse.tick)
                            {
                                priceTickList.Add(tick);
                            }
                        }
                        return(priceTickList);
                    }
                }

                if (response.getPositionResponse != null)
                {
                    if (stream)
                    {
                        if (response.getPositionResponse.timestamp != null)
                        {
                            listener.timestampEvent(response.getPositionResponse.timestamp);
                        }
                        if (response.getPositionResponse.accounting != null)
                        {
                            listener.accountingEvent(response.getPositionResponse.accounting);
                        }
                        if (response.getPositionResponse.assetposition != null)
                        {
                            listener.assetPositionEvent(response.getPositionResponse.assetposition);
                        }
                        if (response.getPositionResponse.securityposition != null)
                        {
                            listener.securityPositionEvent(response.getPositionResponse.securityposition);
                        }
                        if (response.getPositionResponse.heartbeat != null)
                        {
                            listener.positionHeartbeatEvent(response.getPositionResponse.heartbeat);
                        }
                        if (response.getPositionResponse.message != null)
                        {
                            listener.messageEvent(response.getPositionResponse.message);
                        }
                    }
                    else
                    {
                        positionTick                positiontick             = new positionTick();
                        List <assetPositionTick>    assetPositionTickList    = new List <assetPositionTick>();
                        List <securityPositionTick> securityPositionTickList = new List <securityPositionTick>();
                        if (response.getPositionResponse.accounting != null)
                        {
                            positiontick.accountingTick = response.getPositionResponse.accounting;
                        }
                        if (response.getPositionResponse.assetposition != null)
                        {
                            foreach (assetPositionTick tick in response.getPositionResponse.assetposition)
                            {
                                assetPositionTickList.Add(tick);
                            }
                        }
                        if (response.getPositionResponse.securityposition != null)
                        {
                            foreach (securityPositionTick tick in response.getPositionResponse.securityposition)
                            {
                                securityPositionTickList.Add(tick);
                            }
                        }
                        positiontick.assetPositionTickList    = assetPositionTickList;
                        positiontick.securityPositionTickList = securityPositionTickList;
                        return(positiontick);
                    }
                }

                if (response.getOrderResponse != null)
                {
                    if (stream)
                    {
                        if (response.getOrderResponse.timestamp != null)
                        {
                            listener.timestampEvent(response.getOrderResponse.timestamp);
                        }
                        if (response.getOrderResponse.order != null)
                        {
                            listener.orderEvent(response.getOrderResponse.order);
                        }
                        if (response.getOrderResponse.heartbeat != null)
                        {
                            listener.orderHeartbeatEvent(response.getOrderResponse.heartbeat);
                        }
                        if (response.getOrderResponse.message != null)
                        {
                            listener.messageEvent(response.getOrderResponse.message);
                        }
                    }
                    else
                    {
                        List <orderTick> orderTickList = new List <orderTick>();
                        if (response.getOrderResponse.order != null)
                        {
                            foreach (orderTick tick in response.getOrderResponse.order)
                            {
                                orderTickList.Add(tick);
                            }
                        }
                        return(orderTickList);
                    }
                }

                if (response.setOrderResponse != null)
                {
                    List <orderRequest> orderList = new List <orderRequest>();
                    if (response.setOrderResponse.timestamp != null)
                    {
                        //listener.timestampEvent(response.setOrderResponse.timestamp);
                    }
                    if (response.setOrderResponse.order != null)
                    {
                        foreach (orderRequest tick in response.setOrderResponse.order)
                        {
                            orderList.Add(tick);
                        }
                    }
                    if (response.setOrderResponse.message != null)
                    {
                        //listener.messageEvent(response.setOrderResponse.message);
                    }
                    return(orderList);
                }

                if (response.cancelOrderResponse != null)
                {
                    List <cancelTick> cancelTickList = new List <cancelTick>();
                    if (response.cancelOrderResponse.timestamp != null)
                    {
                        //listener.timestampEvent(response.cancelOrderResponse.timestamp);
                    }
                    if (response.cancelOrderResponse.order != null)
                    {
                        foreach (cancelTick tick in response.cancelOrderResponse.order)
                        {
                            cancelTickList.Add(tick);
                        }
                    }
                    if (response.cancelOrderResponse.message != null)
                    {
                        //listener.messageEvent(response.cancelOrderResponse.message);
                    }
                    return(cancelTickList);
                }

                if (response.modifyOrderResponse != null)
                {
                    List <modifyTick> modifyTickList = new List <modifyTick>();
                    if (response.modifyOrderResponse.timestamp != null)
                    {
                        //listener.timestampEvent(response.modifyOrderResponse.timestamp);
                    }
                    if (response.modifyOrderResponse.order != null)
                    {
                        foreach (modifyTick tick in response.modifyOrderResponse.order)
                        {
                            modifyTickList.Add(tick);
                        }
                    }
                    if (response.modifyOrderResponse.message != null)
                    {
                        //listener.messageEvent(response.modifyOrderResponse.message);
                    }
                    return(modifyTickList);
                }

                if (response.getHistoricalPriceResponse != null)
                {
                    List <candleTick> candleTickList = new List <candleTick>();
                    if (response.getHistoricalPriceResponse.candle != null)
                    {
                        foreach (candleTick tick in response.getHistoricalPriceResponse.candle)
                        {
                            candleTickList.Add(tick);
                        }
                    }
                    if (response.getHistoricalPriceResponse.message != null)
                    {
                        //listener.messageEvent(response.getHistoricalPriceResponse.message);
                    }
                    return(candleTickList);
                }
            }
            return(null);
        }
        catch (WebException)
        {
            //Console.WriteLine(ex.Message);
            return(null);
        }
        catch (SocketException)
        {
            //Console.WriteLine(ex.Message);
            return(null);
        }
        catch (IOException)
        {
            //Console.WriteLine(ioex.Message);
            return(null);
        }
    }
    private Object sendRequest(hftRequest hftrequest, string urlpath, bool stream, AdharaHFTListener listener)
    {
        if (token == null)
        {
            return(null);
        }

        int currentRequestId;

        lock (Lock)
        {
            currentRequestId = requestId;
            requestId++;
        }

        HttpWebRequest httpWebRequest;

        if (stream)
        {
            httpWebRequest = (HttpWebRequest)WebRequest.Create(domain + ":" + request_port + url_stream + urlpath);
        }
        else
        {
            httpWebRequest = (HttpWebRequest)WebRequest.Create(domain + ":" + request_port + url_polling + urlpath);
        }
        JavaScriptSerializer serializer = new JavaScriptSerializer();

        serializer.RegisterConverters(new JavaScriptConverter[] { new NullPropertiesConverter() });
        httpWebRequest.ContentType = "application/json";
        httpWebRequest.Method      = "POST";
        if (ssl)
        {
            httpWebRequest.ClientCertificates.Add(certificate1);
        }


        using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
        {
            streamWriter.WriteLine(serializer.Serialize(hftrequest));
            Console.WriteLine(serializer.Serialize(hftrequest));
        }

        HttpWebResponse httpResponse;

        try
        {
            httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
        }
        catch (Exception ex)
        {
            Console.WriteLine("Unexpected response status: " + ex.Message);
            return(null);
        }
        StreamReader streamReader = new StreamReader(httpResponse.GetResponseStream());

        if (stream)
        {
            Thread t = new Thread(() => handleResponse(streamReader, true, listener));
            t.Name = currentRequestId.ToString();
            streamReaderMap.Add(t, streamReader);
            t.Start();
            Console.WriteLine("Running " + t.Name);
            return(t.Name);
        }
        else
        {
            Object res = handleResponse(streamReader, false, null);
            streamReader.Close();
            return(res);
        }
    }
    public string getPositionBegin(List <string> assets, List <string> securities, List <string> accounts, int interval, AdharaHFTListener listener)
    {
        hftRequest hftrequest = new hftRequest();

        hftrequest.getPosition = new getPositionRequest(user, token, assets, securities, accounts, interval);
        Object res = sendRequest(hftrequest, "/getPosition", true, listener);

        return((string)res);
    }
    public string getOrderBegin(List <string> securities, List <string> tinterfaces, List <string> types, int interval, AdharaHFTListener listener)
    {
        hftRequest hftrequest = new hftRequest();

        hftrequest.getOrder = new getOrderRequest(user, token, securities, tinterfaces, types, interval);
        Object res = sendRequest(hftrequest, "/getOrder", true, listener);

        return((string)res);
    }
    public string getPriceBegin(List <string> securities, List <string> tinterfaces, string granularity, int levels, int interval, AdharaHFTListener listener)
    {
        hftRequest hftrequest = new hftRequest();

        hftrequest.getPrice = new getPriceRequest(user, token, securities, tinterfaces, granularity, levels, interval);
        Object res = sendRequest(hftrequest, "/getPrice", true, listener);

        return((string)res);
    }