public SpotifyConnection(ICache cache, TimeSpan timeout)
 {
     this.session = new Session();
     this.protocol = null;
     this.running = false;
     this.user = null;
     this.userSemaphore = new Semaphore(0, 2);
     this.cache = cache;
     this.timeout = timeout;
 }
        /// <summary>
        /// Login to Spotify using the specified username and password.
        /// </summary>
        /// <param name="username">Username to use.</param>
        /// <param name="password">Corresponding password.</param>
        public void Login(string username, string password)
        {
            if (this.protocol != null)
            {
                throw new InvalidOperationException("Already logged in!");
            }
            this.protocol = this.session.Authenticate(username, password);
            this.user = new Sharpotify.Media.User(username);
            this.protocol.AddListener(this);

            /*Start the thread*/
            /*It only runs one time, then it stops. The first asyncronous call is needed :)*/
            System.Threading.Thread backgroundProcess = new System.Threading.Thread(delegate() { this.Run(); });
            backgroundProcess.Name = "Sharpotify.Background";
            backgroundProcess.IsBackground = true;
            backgroundProcess.Start();
        }
 /// <summary>
 /// Closes the connection to a Spotify server.
 /// </summary>
 public void Close()
 {
     if (this.protocol != null)
     {
         this.protocol.Disconnect();
         this.protocol = null;
     }
     if (this.userSemaphore is IDisposable)
     {
         (this.userSemaphore as IDisposable).Dispose();
     }
 }