Index: trunk/src/org/openstreetmap/josm/data/imagery/GeorefImage.java
===================================================================
--- trunk/src/org/openstreetmap/josm/data/imagery/GeorefImage.java	(revision 3746)
+++ trunk/src/org/openstreetmap/josm/data/imagery/GeorefImage.java	(revision 3747)
@@ -26,5 +26,5 @@
     private static final long serialVersionUID = 1L;
 
-    public enum State { IMAGE, NOT_IN_CACHE, FAILED};
+    public enum State { IMAGE, NOT_IN_CACHE, FAILED}
 
     private WMSLayer layer;
Index: trunk/src/org/openstreetmap/josm/gui/layer/WMSLayer.java
===================================================================
--- trunk/src/org/openstreetmap/josm/gui/layer/WMSLayer.java	(revision 3746)
+++ trunk/src/org/openstreetmap/josm/gui/layer/WMSLayer.java	(revision 3747)
@@ -90,4 +90,8 @@
     private final List<WMSRequest> requestQueue = new ArrayList<WMSRequest>();
     private final List<WMSRequest> finishedRequests = new ArrayList<WMSRequest>();
+    /**
+     * List of request currently being processed by download threads
+     */
+    private final List<WMSRequest> processingRequests = new ArrayList<WMSRequest>();
     private final Lock requestQueueLock = new ReentrantLock();
     private final Condition queueEmpty = requestQueueLock.newCondition();
@@ -420,5 +424,5 @@
                 WMSRequest item = it.next();
                 int priority = getRequestPriority(item);
-                if (priority == -1) {
+                if (priority == -1 || finishedRequests.contains(item) || processingRequests.contains(item)) {
                     it.remove();
                 } else {
@@ -446,6 +450,9 @@
             if (canceled)
                 return null;
-            else
-                return requestQueue.remove(0);
+            else {
+                WMSRequest request = requestQueue.remove(0);
+                processingRequests.add(request);
+                return request;
+            }
 
         } finally {
@@ -459,4 +466,5 @@
         requestQueueLock.lock();
         try {
+            processingRequests.remove(request);
             finishedRequests.add(request);
         } finally {
@@ -468,5 +476,5 @@
         requestQueueLock.lock();
         try {
-            if (!requestQueue.contains(request)) {
+            if (!requestQueue.contains(request) && !finishedRequests.contains(request) && !processingRequests.contains(request)) {
                 requestQueue.add(request);
                 queueEmpty.signalAll();
@@ -491,6 +499,6 @@
             }
         } finally {
+            requestQueueLock.unlock();
             finishedRequests.clear();
-            requestQueueLock.unlock();
         }
     }
@@ -788,7 +796,7 @@
     protected Grabber getGrabber(){
         if(getInfo().getImageryType() == ImageryType.HTML)
-            return new HTMLGrabber(mv, this, Grabber.cache);
+            return new HTMLGrabber(mv, this);
         else if(getInfo().getImageryType() == ImageryType.WMS)
-            return new WMSGrabber(mv, this, Grabber.cache);
+            return new WMSGrabber(mv, this);
         else throw new IllegalStateException("getGrabber() called for non-WMS layer type");
     }
Index: trunk/src/org/openstreetmap/josm/io/imagery/Grabber.java
===================================================================
--- trunk/src/org/openstreetmap/josm/io/imagery/Grabber.java	(revision 3746)
+++ trunk/src/org/openstreetmap/josm/io/imagery/Grabber.java	(revision 3747)
@@ -23,5 +23,5 @@
     protected volatile boolean canceled;
 
-    Grabber(MapView mv, WMSLayer layer, CacheFiles cache) {
+    Grabber(MapView mv, WMSLayer layer) {
         this.mv = mv;
         this.layer = layer;
@@ -50,5 +50,5 @@
     }
 
-    abstract void fetch(WMSRequest request) throws Exception; // the image fetch code
+    abstract void fetch(WMSRequest request, int attempt) throws Exception; // the image fetch code
 
     int width(){
@@ -86,5 +86,5 @@
                 if (!layer.requestIsValid(request))
                     return;
-                fetch(request);
+                fetch(request, i);
                 break; // break out of the retry loop
             } catch (Exception e) {
Index: trunk/src/org/openstreetmap/josm/io/imagery/HTMLGrabber.java
===================================================================
--- trunk/src/org/openstreetmap/josm/io/imagery/HTMLGrabber.java	(revision 3746)
+++ trunk/src/org/openstreetmap/josm/io/imagery/HTMLGrabber.java	(revision 3747)
@@ -14,18 +14,17 @@
 import org.openstreetmap.josm.gui.MapView;
 import org.openstreetmap.josm.gui.layer.WMSLayer;
-import org.openstreetmap.josm.io.CacheFiles;
 
 public class HTMLGrabber extends WMSGrabber {
     public static final StringProperty PROP_BROWSER = new StringProperty("imagery.wms.browser", "webkit-image {0}");
 
-    public HTMLGrabber(MapView mv, WMSLayer layer, CacheFiles cache) {
-        super(mv, layer, cache);
+    public HTMLGrabber(MapView mv, WMSLayer layer) {
+        super(mv, layer);
     }
 
     @Override
-    protected BufferedImage grab(URL url) throws IOException {
+    protected BufferedImage grab(URL url, int attempt) throws IOException {
         String urlstring = url.toExternalForm();
 
-        System.out.println("Grabbing HTML " + url);
+        System.out.println("Grabbing HTML " + (attempt > 1? "(attempt " + attempt + ") ":"") + url);
 
         ArrayList<String> cmdParams = new ArrayList<String>();
Index: trunk/src/org/openstreetmap/josm/io/imagery/WMSGrabber.java
===================================================================
--- trunk/src/org/openstreetmap/josm/io/imagery/WMSGrabber.java	(revision 3746)
+++ trunk/src/org/openstreetmap/josm/io/imagery/WMSGrabber.java	(revision 3747)
@@ -27,10 +27,9 @@
 import org.openstreetmap.josm.data.coor.EastNorth;
 import org.openstreetmap.josm.data.coor.LatLon;
+import org.openstreetmap.josm.data.imagery.GeorefImage.State;
 import org.openstreetmap.josm.data.imagery.ImageryInfo;
-import org.openstreetmap.josm.data.imagery.GeorefImage.State;
 import org.openstreetmap.josm.data.projection.Mercator;
 import org.openstreetmap.josm.gui.MapView;
 import org.openstreetmap.josm.gui.layer.WMSLayer;
-import org.openstreetmap.josm.io.CacheFiles;
 import org.openstreetmap.josm.io.OsmTransferException;
 import org.openstreetmap.josm.io.ProgressInputStream;
@@ -42,6 +41,6 @@
     private final boolean urlWithPatterns;
 
-    public WMSGrabber(MapView mv, WMSLayer layer, CacheFiles cache) {
-        super(mv, layer, cache);
+    public WMSGrabber(MapView mv, WMSLayer layer) {
+        super(mv, layer);
         this.baseURL = layer.getInfo().getURL();
         /* URL containing placeholders? */
@@ -50,5 +49,5 @@
 
     @Override
-    void fetch(WMSRequest request) throws Exception{
+    void fetch(WMSRequest request, int attempt) throws Exception{
         URL url = null;
         try {
@@ -57,5 +56,5 @@
                     b.max.east(), b.max.north(),
                     width(), height());
-            request.finish(State.IMAGE, grab(url));
+            request.finish(State.IMAGE, grab(url, attempt));
 
         } catch(Exception e) {
@@ -165,6 +164,6 @@
     }
 
-    protected BufferedImage grab(URL url) throws IOException, OsmTransferException {
-        System.out.println("Grabbing WMS " + url);
+    protected BufferedImage grab(URL url, int attempt) throws IOException, OsmTransferException {
+        System.out.println("Grabbing WMS " + (attempt > 1? "(attempt " + attempt + ") ":"") + url);
 
         HttpURLConnection conn = (HttpURLConnection) url.openConnection();
@@ -193,12 +192,15 @@
         InputStream in = conn.getInputStream();
         BufferedReader br = new BufferedReader(new InputStreamReader(in));
-
-        String line = null;
-        while( (line = br.readLine()) != null) {
-            // filter non-ASCII characters and control characters
-            exception.append(line.replaceAll("[^\\p{Print}]", ""));
-            exception.append('\n');
-        }
-        return exception.toString();
+        try {
+            String line = null;
+            while( (line = br.readLine()) != null) {
+                // filter non-ASCII characters and control characters
+                exception.append(line.replaceAll("[^\\p{Print}]", ""));
+                exception.append('\n');
+            }
+            return exception.toString();
+        } finally {
+            br.close();
+        }
     }
 }
Index: trunk/src/org/openstreetmap/josm/io/imagery/WMSRequest.java
===================================================================
--- trunk/src/org/openstreetmap/josm/io/imagery/WMSRequest.java	(revision 3746)
+++ trunk/src/org/openstreetmap/josm/io/imagery/WMSRequest.java	(revision 3747)
@@ -4,5 +4,4 @@
 import java.awt.image.BufferedImage;
 
-import org.openstreetmap.josm.data.imagery.GeorefImage;
 import org.openstreetmap.josm.data.imagery.GeorefImage.State;
 
