public TestDownloadEngine(int port, string filePath)
        {
            TcpListener listener = new TcpListener(IPAddress.Any, port);

            listener.Start();

            NetworkEngine.Client client = new NetworkEngine.Client(listener.AcceptTcpClient(), false);

            if (client != null)
            {
                DownloadEngine downloadEngine      = new DownloadEngine(client);
                DownloadEngine.UploadStatus status = downloadEngine.SendFile(filePath);
                switch (status)
                {
                case DownloadEngine.UploadStatus.success:
                    Debug.Log("Success");
                    break;

                case DownloadEngine.UploadStatus.alreadyWorking:
                    Debug.LogError("Already working");
                    break;

                case DownloadEngine.UploadStatus.fileDoesntExist:
                    Debug.LogError("File doesn't exist");
                    break;

                case DownloadEngine.UploadStatus.unknownError:
                    Debug.LogError("Unknown Error");
                    break;

                default:
                    break;
                }
            }
        }
        private void ListenForConnections()
        {
            TcpListener listener = new TcpListener(IPAddress.Any, port);

            listener.Start();

            while (true)
            {
                NetworkEngine.Client client = new NetworkEngine.Client(listener.AcceptTcpClient(), true);
                client.dataReceivedFunction = TokenDataReceived;
                //UserAcceptStates state = AcceptUser(client);
            }
        }
        public DownloadEngine(NetworkEngine.Client _client, int _port = -1, string _token = "")
        {
            client = _client;

            port = _port;

            token = _token;


            user = StoreServer.FindUserByToken(token);

            client.dataReceivedFunction = DataReceived;
        }
        private void UserAddError(NetworkEngine.Client client, UserAcceptStates status)
        {
            switch (status)
            {
            case UserAcceptStates.unknown:
                client.Send("UN", "ERROR");
                break;

            case UserAcceptStates.badToken:
                client.Send("BT", "ERROR");
                break;

            default:
                break;
            }
        }
        private void TokenDataReceived(string data, NetworkEngine.Client client)
        {
            if (data.StartsWith("TOKEN") && data.Length > "TOKEN".Length)
            {
                string           token  = data.Remove(0, "TOKEN".Length);
                UserAcceptStates status = AcceptUser(token);
                switch (status)
                {
                case UserAcceptStates.unknown:
                    Debug.LogError("Unknown error adding user with token \"" + token + "\"");
                    UserAddError(client, UserAcceptStates.unknown);
                    break;

                case UserAcceptStates.accepted:
                    AddUser(client, token);
                    break;

                case UserAcceptStates.badToken:
                    Debug.LogError("Bad token: \"" + token + "\"");
                    UserAddError(client, UserAcceptStates.badToken);
                    break;

                case UserAcceptStates.notConnected:
                    Debug.LogError("Client not connected");
                    UserAddError(client, UserAcceptStates.notConnected);
                    break;

                default:
                    break;
                }
            }
            else
            {
                Debug.LogWarning("Message is not token");
            }
        }
 private void AddUser(NetworkEngine.Client client, string token)
 {
     client.Send("OK", "DAUTH");
     DownloadEngine newDownloadClient = new DownloadEngine(client, port, token);
 }
        public void DataReceived(string data, NetworkEngine.Client client)
        {
            string response     = "UK";
            string responseCode = "UNKNW";

            Console.WriteLine("DownloadEngine: Data received: " + data);
            string command = data;

            if (data.Length > 5)
            {
                command = data.Remove(5);
            }

            data = data.Remove(0, 5);

            switch (command)
            {
            case "OK":
                return;

            //Download
            case "DWNLD":
            {
                if (user == null)
                {
                    response     = "NA";
                    responseCode = "ERROR";
                    break;
                }
                if (data.Length == 0)
                {
                    response     = "NF";
                    responseCode = "ERROR";
                    break;
                }
                Int64 id = -1;
                if (!Int64.TryParse(data, out id))
                {
                    Debug.LogError("Cannot parse \"" + data + "\" to Int64");

                    response     = "NF";
                    responseCode = "ERROR";
                    break;
                }

                StoreServer.Game game = StoreServer.FindGame(id);

                if (game == null)
                {
                    response     = "NF";
                    responseCode = "ERROR";
                    break;
                }

                if (!StoreServer.CheckIfUserHaveGame(user, game))
                {
                    response     = "NA";
                    responseCode = "ERROR";
                    break;
                }



                UploadStatus status = SendFile(DownloadsManager.downloadFilesDirectory + game.filename, true, false);

                switch (status)
                {
                case UploadStatus.success:
                    Debug.Log("File sent successfully");
                    client.Send("OK", "DWNLD");
                    workingThread.Start();

                    return;

                case UploadStatus.alreadyWorking:
                    Debug.Log("Thread is busy");
                    break;

                case UploadStatus.fileDoesntExist:
                    response     = "NF";
                    responseCode = "ERROR";
                    break;

                case UploadStatus.unknownError:

                    break;

                default:
                    break;
                }
            }

            break;

            //Game icon
            case "GICON":
            {
                int resLevel = -1;
                if (data.Length == 0)
                {
                    response     = "NF";
                    responseCode = "ERROR";
                    break;
                }

                if (data[0] == 'H' || data[0] == 'h')
                {
                    resLevel = 1;
                }
                else if (data[0] == 'L' || data[0] == 'l')
                {
                    resLevel = 0;
                }
                else
                {
                    response     = "BS";
                    responseCode = "ERROR";
                    break;
                }

                data = data.Remove(0, 1);

                if (user == null)
                {
                    //Not authorised
                    response     = "NA";
                    responseCode = "ERROR";
                    break;
                }
                if (data.Length == 0)
                {
                    response     = "NF";
                    responseCode = "ERROR";
                    break;
                }
                Int64 id = -1;
                if (!Int64.TryParse(data, out id))
                {
                    Debug.LogError("Cannot parse \"" + data + "\" to Int64");

                    response     = "NF";
                    responseCode = "ERROR";
                    break;
                }

                StoreServer.Game game = StoreServer.FindGame(id);

                if (game == null)
                {
                    //Not found
                    response     = "NF";
                    responseCode = "ERROR";
                    break;
                }

                if (game.iconPath == "\0")
                {
                    //No icon
                    response     = "NI";
                    responseCode = "ERROR";
                    break;
                }



                UploadStatus status = SendFile(DownloadsManager.downloadIconsDirectory + game.iconPath + "_" + resLevel.ToString() + ".png", true, false);

                switch (status)
                {
                case UploadStatus.success:
                    Debug.Log("File sent successfully");
                    client.Send("OK", "DWNLD");
                    workingThread.Start();

                    return;

                case UploadStatus.alreadyWorking:
                    Debug.Log("Thread is busy");
                    break;

                case UploadStatus.fileDoesntExist:
                    response     = "NF";
                    responseCode = "ERROR";
                    break;

                case UploadStatus.unknownError:

                    break;

                default:
                    break;
                }
            }
            break;

            //Not found
            default:
                response     = "NF";
                responseCode = "ERROR";
                break;
            }
            Debug.LogWarning("Sending response ");
            client.Send(response, responseCode);
        }